Table of Contents

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

member MemberInfo

The property or field to wrap.

Exceptions

NotSupportedException

Thrown when member is 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

bool

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

bool

DeclaringType

Gets the class that declares this member.

public override Type DeclaringType { get; }

Property Value

Type

The Type object 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

bool

IsProperty

Gets a value indicating whether this member is a property (PropertyInfo).

public bool IsProperty { get; }

Property Value

bool

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

bool

IsStatic

Gets a value indicating whether this member is static.

public bool IsStatic { get; }

Property Value

bool

Member

Gets the underlying MemberInfo representing either a property or a field.

public MemberInfo Member { get; }

Property Value

MemberInfo

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

int

A value which, in combination with Module, uniquely identifies a metadata element.

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

string

A string containing the name of this member.

ReflectedType

Gets the class object that was used to obtain this instance of MemberInfo.

public override Type ReflectedType { get; }

Property Value

Type

The Type object through which this MemberInfo object was obtained.

Type

Gets the type of the property or field.

public Type Type { get; }

Property Value

Type

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

type Type

The attribute type to find.

Returns

Attribute

A matching Attribute or null.

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 T or null.

Type Parameters

T

The 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

inherit bool

Whether to search the inheritance chain.

Returns

T

A matching attribute of type T or null.

Type Parameters

T

The 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

inherit bool

true 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

attributeType Type

The attribute type to find.

Returns

Attribute[]

An array of Attribute instances.

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

attributeType Type

The type of attribute to search for. Only attributes that are assignable to this type are returned.

inherit bool

true 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 attributeType have been applied.

Exceptions

TypeLoadException

A custom attribute type cannot be loaded.

ArgumentNullException

If attributeType is 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

T

The 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

instance object

The 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

attributeType Type

The type of custom attribute to search for. The search includes derived types.

inherit bool

true 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 attributeType or 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

instance object

The object whose member value will be modified.

value object

The 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

fieldInfo FieldInfo

The 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

propertyInfo PropertyInfo

The 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

propertyOrFieldInfo PropertyOrFieldInfo

The wrapper to unwrap.

Returns

FieldInfo

The underlying FieldInfo, or null if the wrapper holds a property.

implicit operator PropertyInfo(PropertyOrFieldInfo)

Implicit conversion from PropertyOrFieldInfo to PropertyInfo.

public static implicit operator PropertyInfo(PropertyOrFieldInfo propertyOrFieldInfo)

Parameters

propertyOrFieldInfo PropertyOrFieldInfo

The wrapper to unwrap.

Returns

PropertyInfo

The underlying PropertyInfo, or null if the wrapper holds a field.