I'm trying to serialise all the properties of an object that by default cannot be serialised (the object is of type SPGroup which is sealed and has no empty constructor) so I'm copying all of the properties from the object which I will then serialise.
I get the data from the properties with the code below:
private dynamic GetSerializableProperties<T>(T objToCopy, dynamic dataContainer = null)
{
// Copy the properties into dataContainer, if it's not set
if (Object.ReferenceEquals(null, dataContainer))
{
dataContainer = new ExpandoObject() as IDictionary<string, object>;
}
// copy base class properties.
var objToCopyType = objToCopy.GetType();
foreach (PropertyInfo propertyInfo in objToCopyType.GetProperties())
{
// Get the correct property
PropertyInfo propertyInfoValue = objToCopyType.GetProperty(propertyInfo.Name);
// Get the value of the property
var value = objToCopyType.GetProperty(propertyInfo.Name).GetValue(objToCopy, null); // propertyInfoValue.GetValue(objToCopy, null);
// Create the ID for the dictionary, set to name/random string for easy viewing
string propertyKeyName = CreateSafeName(dataContainer, propertyInfo.Name);
// Create a property with the required name then add data
((IDictionary<String, Object>)dataContainer)[propertyInfo.Name] = new DataContainer(propertyInfo.Name, propertyInfo, propertyInfoValue, value);
}
return dataContainer;
}
When I look into the dynamic object I've created I see all the properties I want i.e. the Property Users contains a list of users. The next step is serialising this data which is where the error occurs.
dynamic serializableObject = GetSerializableProperties<T>(serializableObjectx);
var test = serializableObject as IDictionary<string, Object>;
var test2 = test.Values.ElementAt(2) as DataContainer; // This property contains a property that is causing the issue I believe
var obj = JsonConvert.SerializeObject(test2, Newtonsoft.Json.Formatting.Indented);
The error I get is:
Cannot evaluate expression because the current thread is in a stack overflow state.
I believe the code below this comment needs to be changed:
var value = objToCopyType.GetProperty(propertyInfo.Name).GetValue(objToCopy, null); // propertyInfoValue.GetValue(objToCopy, null);
If I get you right you are trying to serialize sharepoint objects? Most of the time it will end in a circulation.
SPGroup.Users -> SPUser.Groups -> SPGroup.Users -> ...
So the serializer will run in a infinite loop or a stack overflow. The better way will be building your own objects without some of the properties which leads to a circle.
Related
In C# I have multiple instantiations of a class "CItems"(see below). I retrieve a string at run-time of the instantiation I want to use(in this case to call a public method-"addPropertyToList"). I know I have to use reflection but I can't seem to get it right.
CItems me = new CItems();
CItems conversations = new CItems();
string whichCItem = "me"
properties = <whichCItem>.addPropertyToList(properties, "FirstName", "Ken");
I tried many things like:
var myobject = this;
string propertyname = "me";
PropertyInfo property = myobject.GetType().GetProperty(propertyname);
object value = property.GetValue(myobject, null);
But that resulted in:
Object reference not set to an instance of an object. Because property ends up null.
Thanks for any help and please be gentle. I really have no idea what I am doing and I may have used some wrong terminology.
A simple Dictionary<T, U> might work for you.
Consider an example:
CItems me = new CItems();
CItems conversations = new CItems();
...
var dic = new Dictionary<string, CITems>();
doc.Add("me", me);
doc.Add("conversations", conversations);
...
//find object
CITems result= null;
dic.TryGetValue(searchString, out result);
PropertyInfo property = myobject.GetType().GetProperty(propertyname);
This is the correct approach for tretrieving the property identified by propertyname. You already know the type it is declared on, so you just use
var propertyInfo = CItems.GetProperty(propertyname)
to retrieve the class property. What you now need to do is set that property on the identified instance, so that you can call
propertyInfo.SetValue(<instance>, value);
How are your instances identified? Surely you are not giving back the name of the variable in which the object pointer is stored?
Is something like the following achievable?
IEnumerable<CItems> myItems = new { new CItem("me"), new CItem("conversations") }
void somemethod(string instanceName, string propertyname)
{
var instance = myItems.FirstOrDefault(item => item.Name == instanceName);
if(instance == null) return;
var propertyInfo = CItems.GetProperty(propertyname);
propertyInfo.SetValue(instance, value);
}
I have a library that is doing a bunch of reflection work relying on the PropertyInfo of the classes it receives (to get and set values).
Now I want to be able to work with dynamic objects, but I can't find how to get the PropertyInfo of a dynamic's properties. I've checked the alternatives, but for those I'd need to change everywhere I use PropertyInfo to get/set values.
dynamic entity = new ExpandoObject();
entity.MyID = 1;
// - Always null
PropertyInfo p = entity.GetType().GetProperty("MyID");
// - Always null
PropertyInfo[] ps = entity.GetType().GetProperties();
// - These are called everywhere in the code
object value = p.GetValue(entity);
p.SetValue(entity, value);
Is it possible to get or create a PropertyInfo somehow just to be able to use it's GetValue() and SetValue() on a dynamic object?
Under the covers an ExpandoObject is really just a dictionary. You can get at the dictionary just by casting it.
dynamic entity = new ExpandoObject();
entity.MyID = 1;
if(entity.GetType() == typeof(ExpandoObject))
{
Console.WriteLine("I'm dynamic, use the dictionary");
var dictionary = (IDictionary<string, object>)entity;
}
else
{
Console.WriteLine("Not dynamic, use reflection");
}
You could modify your Mapping method to check if the object being passed in is dynamic and route through a different path that just iterates over the keys of the dictionary.
https://dotnetfiddle.net/VQQZdy
So I have 2 classes, both have identical Property names. One class contains different variables: int, strings, bool and DateTime The second class contains only 1 int and the rest are all strings.
Now I want to loop through all the properties, get the value from class1, encrypt that data and save it as a string in obj2, then return it to the main form (to save it in a database later).
public PersoonEncrypted EncryptPersonClass(Class1 object1)
{
PersoonEncrypted persEncrypt = new PersoonEncrypted(); //second class obj
Type type = object1.GetType();
PropertyInfo[] properties = type.GetProperties();
Type type2 = persEncrypt.GetType();
PropertyInfo[] properties2 = type.GetProperties();
foreach (var bothProperties in properties.Zip(properties2, (obj1, obj2) => new { Obj1 = obj1, Obj2 = obj2 }))
{
string value = "";
value = bothProperties.Obj1.GetValue(object1) as string;
if (!string.IsNullOrWhiteSpace(value))
{
string encryptValue = Encrypt(value);
if ((bothProperties.Obj2 != null) && (bothProperties.Obj2.PropertyType == typeof(string)))
{ //!= null check has no effect at all
bothProperties.Obj2.SetValue(persEncrypt, encryptValue, null); //errorLine
}
}
}
return persEncrypt;
}
That is what I came up with until now.
I have, of course, searched for other solutions like this one. This, after applying some own changes, didn't return any errors, but it didn't save any encrypted strings into the class persEncrypt. What I concluded was, from that test, is that it was testing if the value in the second class(persEncrypt in my example) from the particular property was null, while it shouldn't do that, it should make a new instance of that variable and save it in the object class, but removing that check gave me the same error.
you're just .Zip-ing the two lists of PropertyInfo objects, which simply iterates through both lists and doesn't check or sort for any sort of matching. This could result in erroneous behavior depending on the order in which properties appear - consider using a .Join instead to match property names.
This code doesn't check for an indexer on the property before attempting to assign to it without one - any indexed property which is of type string will make it to this point and then throw an exception when you try to set it.
Because this code is calling into Properties, there's the possibility an exception is being thrown by the code of the Property itself. This is where a StackTrace from your exception could reveal much more about what's happening.
Your code also checks for a property of type string directly - when using reflection you should use IsAssignableFrom instead in order to allow for inherited types, though that is unlikely the issue in this one case.
I have the following method to return a Dictionary<string, string> with the names of all public members (fields and properties) of an object as the dictionary key. I can get the name of the members, but I can't get their values. Could anyone tell me how to achieve this in the method below:
public Dictionary<String, String> ObjectProperty(object objeto)
{
Dictionary<String, String> dictionary = new Dictionary<String, String>();
Type type = objeto.GetType();
FieldInfo[] field = type.GetFields();
PropertyInfo[] myPropertyInfo = type.GetProperties();
String value = null;
foreach (var propertyInfo in myPropertyInfo)
{
value = (string)propertyInfo.GetValue(this, null); //Here is the error
dictionary.Add(propertyInfo.Name.ToString(), value);
}
return dictionary;
}
Error:
Object does not match target type.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.Reflection.TargetException: Object does not match target type.
Two things here:
You're passing in this, instead of objeto, which means you're trying to read the properties off of the wrong object.
You're not ensuring that you're only trying to read properties that aren't indexers.
Try changing the foreach to this:
foreach (var propertyInfo in myPropertyInfo)
{
if (propertyInfo.GetIndexParameters().Length == 0)
{
value = (string) propertyInfo.GetValue(objeto, null);
dictionary.Add(propertyInfo.Name.ToString(), value);
}
}
A note, here:
foreach (var propertyInfo in myPropertyInfo)
{
value = (string) propertyInfo.GetValue(this, null); //Here is the error
dictionary.Add(propertyInfo.Name.ToString(), value);
}
You are assuming that ALL your properties are strings. Are they?
If they aren't, but you want strings anyway, you can use this code:
object objValue = propertyInfo.GetValue(objeto, null);
value = (objValue == null) ? null : objValue.ToString();
The above code also takes into consideration that property values may be null. I didn't take into consideration the possibility of indexed properties, but if you have any, you'll need to accommodate them.
Also, as Lasse V. Karlsen has pointed out, by passing this instead of objeto, you are trying to pull property values from the parent class of the method, not objeto. If they aren't the same object, you won't get the results you want; if they aren't even the same type of object, then you'll get an error.
Finally, you've used the term "attributes", which refers to something other than properties in .NET, and also you've referred to class variables, which are also not properties. Are the properties actually what you want, as opposed to "fields" or attributes applied to the definition of the class?
In my precent questions, I want to retrieve some values via reflection.
Now I want set values to objects thanks to reflection.
I want to write this :
private void AppliquerColonnesPersonnalisation(Control control, Propriete propriete, PropertyInfo Info)
{
UltraGrid grille = (UltraGrid)control;
SortedList<int,string> sortedOrderedColumns = new SortedList<int,string>();
if (grille != null)
{
// I want to write MapPropertyInfo method
ColumnsCollection cols = MapPropertyInfo(Info);
PropertyInfo contains a type of ColumnsCollection. I just want to "map" my PropertyInfo to an object to define some properties after : For example :
cols[prop.Nom].Hidden = false;
Is it possible ?
Best Regards,
Florian
EDIT : I tried the GenericTypeTea solution, but I have some problem. Here my code snippet :
private void AppliquerColonnesPersonnalisation(Control control, Propriete propriete, PropertyInfo Info)
{
UltraGrid grille = (UltraGrid)control;
ColumnsCollection c = grille.DisplayLayout.Bands[0].Columns;
// Throw a not match System.Reflection.TargetException
ColumnsCollection test = Info.GetValue(c,null) as ColumnsCollection;
SortedList<int,string> sortedOrderedColumns = new SortedList<int,string>();
But a TargetException is Thrown
So you already have a PropertyInfo object that is of type ColumnsCollection?
You can get it and modify it using the following code:
var original = GetYourObject();
PropertyInfo Info = GetYourPropertyInfo(original);
ColumnsCollection collection = Info.GetValue(original) as ColumnsCollection;
Basically, you just need to pass your original object back into the PropertyInfo's GetValue method which will return you an object. Just cast that as the ColumnsCollection and you should be sorted.
UPDATE:
Based on your update, you should be doing this:
object original = grille.DisplayLayout.Bands[0];
PropertyInfo info = original.GetProperty("Columns");
ColumnsCollection test = info.GetValue(original, null) as ColumnsCollection;
You must be getting your Info PropertyInfo from an object of a different type. Although I think we're fixing the wrong problem here. I don't understand what you're trying to achieve. Why not just modify grille.DisplayLayout.Bands[0].Columns directly?