Check if a property exists in a class

后端 未结 6 1402
面向向阳花
面向向阳花 2020-11-29 03:04

I try to know if a property exist in a class, I tried this :

public static bool HasProperty(this object obj, string propertyName)
{
    return obj.GetType().         


        
6条回答
  •  囚心锁ツ
    2020-11-29 03:59

    I got this error: "Type does not contain a definition for GetProperty" when tying the accepted answer.

    This is what i ended up with:

    using System.Reflection;
    
    if (productModel.GetType().GetTypeInfo().GetDeclaredProperty(propertyName) != null)
    {
    
    }
    

提交回复
热议问题