0

Example:

    public class MyClass : AbstractClass<UserDTO>
    {
        public MyClass()
        {
        }

        public string Property1 { get; set; }

        public string Property2 { get; set; }
    }

Is it even possible to get the type of MyClass just by examining the UserDTO object? If yes, how? TIA.

Dennis Laping
  • 682
  • 1
  • 7
  • 18

2 Answers2

1

I think you are asking something like:

Is there a way within AbstractClass<T> that, where the type of T is UserDTO, I can determine the type of class X where class X : AbstractClass<UserDTO>

If this is indeed your question, then the short answer is "yes". The path to achieving it is a little convoluted though.

First step, obtain a list of all the types in your system, using eg https://stackoverflow.com/a/4692463/7122.

Next step, for each type use the BaseType property to get its parent type. If that type is AbstractClass then examine its GenericTypeArguments property to see if that is UserDTO. If it is, that's your type.

More than one class may inherit from AbstractClass<UserDTO> though, so you'd have to examine all types to check for that, rather than stopping at the first match.

Community
  • 1
  • 1
David Arno
  • 42,717
  • 16
  • 86
  • 131
0

If you have an instance of a UserDTO class and nothing else then I can't see any way that you'd be able to see the owning instance or type of MyClass - how would you differentiate between that and a UserDTO which wasn't on a MyClass? Sorry

Stu
  • 2,426
  • 2
  • 26
  • 42