Class PropertyOrFieldInfo
- Namespace
- Utils.Reflection
- Assembly
- Utils.dll
Represents a reflection wrapper around either a PropertyInfo or a FieldInfo, providing unified access to both for getting and setting values, as well as attribute retrieval.
public class PropertyOrFieldInfo : MemberInfo, ICustomAttributeProvider
- Inheritance
-
PropertyOrFieldInfo
- Implements
- Inherited Members
- Extension Methods
Constructors
PropertyOrFieldInfo(MemberInfo)
Initializes a new instance of the PropertyOrFieldInfo class, wrapping either a PropertyInfo or a FieldInfo.
public PropertyOrFieldInfo(MemberInfo member)
Parameters
memberMemberInfoThe property or field to wrap.
Exceptions
- NotSupportedException
Thrown when
memberis neither PropertyInfo nor FieldInfo.
Properties
CanRead
Gets a value indicating whether this member can be read (always true for fields, or CanRead for properties).
public bool CanRead { get; }
Property Value
CanWrite
Gets a value indicating whether this member can be written to (false for read-only fields or write-restricted properties).
public bool CanWrite { get; }
Property Value
DeclaringType
Gets the class that declares this member.
public override Type DeclaringType { get; }
Property Value
- Type
The
Typeobject for the class that declares this member.
IsCollectible
Gets a value that indicates whether this MemberInfo object is part of an assembly held in a collectible AssemblyLoadContext.
public override bool IsCollectible { get; }
Property Value
- bool
true if the MemberInfo is part of an assembly held in a collectible assembly load context; otherwise, false.
IsField
Gets a value indicating whether this member is a field (FieldInfo).
public bool IsField { get; }
Property Value
IsProperty
Gets a value indicating whether this member is a property (PropertyInfo).
public bool IsProperty { get; }
Property Value
IsReadOnly
Gets a value indicating whether this member is read-only (i.e., has no setter or is an init-only/const field).
public bool IsReadOnly { get; }
Property Value
IsStatic
Gets a value indicating whether this member is static.
public bool IsStatic { get; }
Property Value
Member
Gets the underlying MemberInfo representing either a property or a field.
public MemberInfo Member { get; }
Property Value
MemberType
When overridden in a derived class, gets a MemberTypes value indicating the type of the member - method, constructor, event, and so on.
public override MemberTypes MemberType { get; }
Property Value
- MemberTypes
A MemberTypes value indicating the type of member.
MetadataToken
Gets a value that identifies a metadata element.
public override int MetadataToken { get; }
Property Value
Exceptions
- InvalidOperationException
The current MemberInfo represents an array method, such as
Address, on an array type whose element type is a dynamic type that has not been completed. To get a metadata token in this case, pass the MemberInfo object to the System.Reflection.Emit.ModuleBuilder.GetMethodToken(System.Reflection.MethodInfo) method; or use the System.Reflection.Emit.ModuleBuilder.GetArrayMethodToken(System.Type,System.String,System.Reflection.CallingConventions,System.Type,System.Type[]) method to get the token directly, instead of using the GetArrayMethod(Type, string, CallingConventions, Type, Type[]) method to get a MethodInfo first.
Module
Gets the module in which the type that declares the member represented by the current MemberInfo is defined.
public override Module Module { get; }
Property Value
- Module
The Module in which the type that declares the member represented by the current MemberInfo is defined.
Exceptions
- NotImplementedException
This method is not implemented.
Name
Gets the name of the current member.
public override string Name { get; }
Property Value
ReflectedType
Gets the class object that was used to obtain this instance of MemberInfo.
public override Type ReflectedType { get; }
Property Value
- Type
The
Typeobject through which thisMemberInfoobject was obtained.
Type
Gets the type of the property or field.
public Type Type { get; }
Property Value
Methods
GetCustomAttribute(Type)
Retrieves the custom attribute of the specified type on this member.
Returns null if not found.
public Attribute GetCustomAttribute(Type type)
Parameters
typeTypeThe attribute type to find.
Returns
GetCustomAttribute<T>()
Retrieves the first custom attribute of type T or null if not found.
public T GetCustomAttribute<T>() where T : Attribute
Returns
- T
A matching attribute of type
Tornull.
Type Parameters
TThe attribute type to find.
GetCustomAttribute<T>(bool)
Retrieves the first custom attribute of type T or null if not found.
public T GetCustomAttribute<T>(bool inherit) where T : Attribute
Parameters
inheritboolWhether to search the inheritance chain.
Returns
- T
A matching attribute of type
Tornull.
Type Parameters
TThe attribute type to find.
GetCustomAttributes()
Retrieves all custom attributes on this member.
public IEnumerable<Attribute> GetCustomAttributes()
Returns
- IEnumerable<Attribute>
An enumeration of attributes.
GetCustomAttributes(bool)
When overridden in a derived class, returns an array of all custom attributes applied to this member.
public override Attribute[] GetCustomAttributes(bool inherit)
Parameters
inheritbooltrue to search this member's inheritance chain to find the attributes; otherwise, false. This parameter is ignored for properties and events.
Returns
- Attribute[]
An array that contains all the custom attributes applied to this member, or an array with zero elements if no attributes are defined.
Exceptions
- InvalidOperationException
This member belongs to a type that is loaded into the reflection-only context. See How to: Load Assemblies into the Reflection-Only Context.
- TypeLoadException
A custom attribute type could not be loaded.
GetCustomAttributes(Type)
Retrieves all custom attributes of the specified type.
public Attribute[] GetCustomAttributes(Type attributeType)
Parameters
attributeTypeTypeThe attribute type to find.
Returns
GetCustomAttributes(Type, bool)
When overridden in a derived class, returns an array of custom attributes applied to this member and identified by Type.
public override Attribute[] GetCustomAttributes(Type attributeType, bool inherit)
Parameters
attributeTypeTypeThe type of attribute to search for. Only attributes that are assignable to this type are returned.
inheritbooltrue to search this member's inheritance chain to find the attributes; otherwise, false. This parameter is ignored for properties and events.
Returns
- Attribute[]
An array of custom attributes applied to this member, or an array with zero elements if no attributes assignable to
attributeTypehave been applied.
Exceptions
- TypeLoadException
A custom attribute type cannot be loaded.
- ArgumentNullException
If
attributeTypeis null.- InvalidOperationException
This member belongs to a type that is loaded into the reflection-only context. See How to: Load Assemblies into the Reflection-Only Context.
GetCustomAttributesData()
Returns a list of CustomAttributeData objects representing data about the attributes that have been applied to the target member.
public override IList<CustomAttributeData> GetCustomAttributesData()
Returns
- IList<CustomAttributeData>
A generic list of CustomAttributeData objects representing data about the attributes that have been applied to the target member.
GetCustomAttributes<T>()
Retrieves all custom attributes of type T.
public IEnumerable<T> GetCustomAttributes<T>() where T : Attribute
Returns
- IEnumerable<T>
An enumeration of attributes of type
T.
Type Parameters
TThe attribute type to find.
GetValue(object)
Gets the value of the wrapped property or field for the specified instance.
public object GetValue(object instance)
Parameters
instanceobjectThe object whose member value will be returned.
Returns
- object
The value of the property or field.
IsDefined(Type, bool)
When overridden in a derived class, indicates whether one or more attributes of the specified type or of its derived types is applied to this member.
public override bool IsDefined(Type attributeType, bool inherit)
Parameters
attributeTypeTypeThe type of custom attribute to search for. The search includes derived types.
inheritbooltrue to search this member's inheritance chain to find the attributes; otherwise, false. This parameter is ignored for properties and events.
Returns
- bool
true if one or more instances of
attributeTypeor any of its derived types is applied to this member; otherwise, false.
SetValue(object, object)
Sets the value of the wrapped property or field for the specified instance.
public void SetValue(object instance, object value)
Parameters
instanceobjectThe object whose member value will be modified.
valueobjectThe new value to assign to the property or field.
Exceptions
- InvalidOperationException
Thrown when trying to set the value of a read-only property or field.
ToString()
Returns a string that represents the current object.
public override string ToString()
Returns
- string
A string that represents the current object.
Operators
implicit operator PropertyOrFieldInfo(FieldInfo)
Implicit conversion from FieldInfo to PropertyOrFieldInfo.
public static implicit operator PropertyOrFieldInfo(FieldInfo fieldInfo)
Parameters
fieldInfoFieldInfoThe field to wrap.
Returns
- PropertyOrFieldInfo
A new PropertyOrFieldInfo instance wrapping the field.
implicit operator PropertyOrFieldInfo(PropertyInfo)
Implicit conversion from PropertyInfo to PropertyOrFieldInfo.
public static implicit operator PropertyOrFieldInfo(PropertyInfo propertyInfo)
Parameters
propertyInfoPropertyInfoThe property to wrap.
Returns
- PropertyOrFieldInfo
A new PropertyOrFieldInfo instance wrapping the property.
implicit operator FieldInfo(PropertyOrFieldInfo)
Implicit conversion from PropertyOrFieldInfo to FieldInfo.
public static implicit operator FieldInfo(PropertyOrFieldInfo propertyOrFieldInfo)
Parameters
propertyOrFieldInfoPropertyOrFieldInfoThe wrapper to unwrap.
Returns
implicit operator PropertyInfo(PropertyOrFieldInfo)
Implicit conversion from PropertyOrFieldInfo to PropertyInfo.
public static implicit operator PropertyInfo(PropertyOrFieldInfo propertyOrFieldInfo)
Parameters
propertyOrFieldInfoPropertyOrFieldInfoThe wrapper to unwrap.
Returns
- PropertyInfo
The underlying PropertyInfo, or
nullif the wrapper holds a field.