-1

I want to have a generic method with a random type parameter.

The instances of T shoud be a models with some attributed properties, so I whant to collect all public properties of an instance.

In additional I want to have an Interface or Superclass for the models and able to use other inheritance stuff on them too.

The problem is the typeof(T) result for models that have been passed as SuperClass or Interface explicitly does have no information about the subclasses.

public interface ISomeInterface { int IProperty { get; }  }

public class SomeClass : ISomeInterface 
{ 
    public int ClassProperty { get; set; } //I need this too

    public int IProperty { get; set; }
}

class Program
{
    static void Main(string[] args)
    {
        var v = (ISomeInterface)new SomeClass();

        TestMethod(v);

        Console.ReadKey();
    }

    static void TestMethod<T>(T value) where T : ISomeInterface
    {
        var props = typeof(T).GetProperties(BindingFlags.Public | BindingFlags.Instance);
        foreach (var p in props)
        {
            Console.WriteLine(p.Name);
        }
    }
}

The output will be the IProperty only, the ClassProperty had been missed.

I can not be shure that values always will have been passed in like subtypes explicitly in future.

Is there are any way to get the runtime type of an instance without using *.GetType() in this case, even for a null references?

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Kamerton
  • 315
  • 3
  • 9
  • 1
    _"Is there are any way to get the runtime type of an instance without using *.GetType() in this case, even for a null refferences?"_ -- your question isn't making any sense. The `GetType()` method is the _only_ way to get the runtime type of an object. Why do you say you don't want to use it? And given that the `null` value can be assigned to any nullable type variable, what would it mean to get the "runtime type" of a `null` value? How is the code supposed to distinguish between a null value for `string` and a null value of `SomeClass`, except via some _compile time_ context? – Peter Duniho Aug 12 '19 at 22:21
  • A little remark - the question is not making any sence for you. If it has been asked then it is somehow. I don't want to use `GetType()` because belived in some other way to do that, to tell that I want to get a recommendation where to find this way or that is not possible / realized. How shoud the code suppose distinquish for `null` - I don't know. May be somehow? Maybe I don't know something. If it can not - ok. It woud be cool to get a refference for some deep topics about it for future. Anyway thanks for reply and patience. – Kamerton Aug 13 '19 at 10:38

1 Answers1

0

Use GetType instead of typeof. The typeof keyword is resolved at compile time, and depends on the type of the variable; GetType() is resolved at runtime and depends on the actual type of the object that was passed.

static void TestMethod<T>(T value) where T : ISomeInterface
{
    var props1 = typeof(T).GetProperties(BindingFlags.Public | BindingFlags.Instance);
    foreach (var p in props1)
    {
        Console.WriteLine("Using typeof(T): {0}", p.Name);
    }
    var props2 = value.GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance);
    foreach (var p in props2)
    {
        Console.WriteLine("Using value.GetType(): {0}", p.Name);
    }
}

Output:

Using typeof(T): IProperty
Using value.GetType(): ClassProperty
Using value.GetType(): IProperty

See the example on DotNetFiddle

See also What is the difference between GetType() and typeof()?

John Wu
  • 50,556
  • 8
  • 44
  • 80