1

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.

Community
  • 1
  • 1
Tizz
  • 820
  • 1
  • 15
  • 31

1 Answers1

2

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 get System.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();
Sriram Sakthivel
  • 72,067
  • 7
  • 111
  • 189
  • Yes, makes total sense what you said, and I know exactly why I am getting System.Object instead of the type I want. Maybe I do have to go about this in a different way. Ive clarified a bit more. Hope it helps explain what Im trying to do. – Tizz Sep 16 '14 at 15:43
  • @Tizz I updated my answer to show how to get that information from instance of `SomeClass`. Without instance, you can't find it for obvious reasons(because you can store whatever at runtime in separate instances). – Sriram Sakthivel Sep 16 '14 at 15:47
  • Thank you, that helps. The current code uses PropertyInfo[] to do a bunch of things, and I am trying to fit my solution into it. I like that I can get the dynamic Type, but I really need a PropertyInfo of that type. Can you continue to help further? – Tizz Sep 16 '14 at 15:54
  • `PropertyInfo` is a abstract class, which doesn't have any public implementation. So you can't create instance yourself. To get a instance of `PropertyInfo` with type that doesn't exist is very hard. If your code is very much coupled with `PropertyInfo` you need to decouple it. May be an interface, or `MyCustomPropertyInfo` class with property named `PropertyType` can help. with that you can return the dynamic property from your implementation. I hope this helps. – Sriram Sakthivel Sep 16 '14 at 15:59
  • I knew there was no answer, just wanted another way to look at it and you did help. I ended up changing the 'Implemented code' to handle a dynamic case. And using the `PropertyType` as you showed above was key in implementing that. Thanks! – Tizz Sep 16 '14 at 19:43