简体   繁体   中英

Check if property is “T” using reflection

I'm working on a project where I need to "translate" a .net class to typescript, bug I'm struggling with the following problem:

I'm trying to know at runtime if the property AnotherClass.property1.genericProperty is type T , but I'm always getting MyClass as type, what is the proper way to check it?

class MyGenericClass<T>
{
    public T genericProperty { get; set; }
}

class AnotherClass
{
    public MyGenericClass<MyClass> property1 { get; set; }
    public MyClass regularProperty { get; set; }
}

Typescript that I expect:

interface MyGenericClass<T> {
    genericProperty: T;
}

What I'm getting at the moment:

interface MyGenericClass<T> {
    genericProperty: MyClass;
}

Following @Mark Gravell comment I was able to solve my problem, what I did:

Used

AnotherClass.property1.PropertyType.GetGenericTypeDefinition().GetProperty("genericProperty").PropertyType.Name

instead of

AnotherClass.property1.PropertyType.GetProperty("genericProperty").PropertyType.Name

And now I'm getting the desired results.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM