I create a dynamic type at run-time (please see how here: Serialize/Deserialize a dynamic object) and use that type to create a dynamic member in another class.
public class SomeClass
{
private dynamic _AnimalType = CreateAnimal<Animal>("Dog");
public dynamic AnimalType { get { return _AnimalType; } }
static internal PropertyInfo[] Sprops = typeof(SomeClass).GetProperties();
static public PropertyInfo[] getPropertyInfos() { get { return Sprops; } }
}
However, when the following is called:
PropertyInfo[] Sprops = typeof(SomeClass).GetProperties();
Sprops contains one PropertyInfo (System.Object
), which is 'exptected' but not what is 'wanted'. I would like to get the Type that AnimalType is currently (Animal
, or more specifically Dog
). Is there any way to accomplish this? I do not want to have to create an instance of this class and then call some "SetInternalProperties" method. The idea is to have the properties readily available as a static.
Yes, PropertyInfo
isn't capable of getting the runtime type of the field. All it does is it gets you the type of the property what is declared.
So it should be something
dynamic
isn't it? Why did you getSystem.Object
?
Well, dynamic is just System.Object
under the covers decorated with System.Runtime.CompilerServices.DynamicAttribute. That's why you just see System.Object
. There is no way to get the runtime information(without the instance).
Is there any way to accomplish this? : If you can explain what you're trying to achieve you may get better answer.
If you have the instance of SomeClass
you can find what is the runtime type.
SomeClass instance = new SomeClass();// get instance somehow
PropertyInfo pi = ...;//Get the property info
Type dynamicType = pi.GetValue(instance).GetType();