This example shows that C # uses reflection to determine whether an object contains an attribute, and for C # program designers, it has a certain learning value.
The specific implementation code is as follows:
1 /// <summary>2 ///use reflection to determine whether an object contains a property3 /// </summary>4 /// <param name= "instance" >Object</param>5 /// <param name= "PropertyName" >properties that need to be judged</param>6 /// <returns>whether it contains</returns>7 Public Static BOOLContainproperty ( This ObjectInstancestringPropertyName)8 {9 if(Instance! =NULL&&!string. IsNullOrEmpty (PropertyName))Ten { OnePropertyInfo _findedpropertyinfo =instance. GetType (). GetProperty (PropertyName); A return(_findedpropertyinfo! =NULL); - } - return false; the}
Here's how to use the code:
1 bool cc = _person. Containproperty ("cc"); 2 bool AA = _person. Containproperty ("Age");
C # uses reflection to determine whether an object contains an implementation of an attribute