Table of Contents

Class RH

Namespace
SunamoReflection
Assembly
SunamoReflection.dll

Reflection helper providing dump, property/field access, and type inspection utilities.

public class RH
Inheritance
RH
Inherited Members
Extension Methods

Methods

AllReferencedAssemblies(Assembly, bool)

Gets all referenced assemblies recursively from the specified assembly.

public static List<string> AllReferencedAssemblies(Assembly entryAssembly, bool isUsingCache = true)

Parameters

entryAssembly Assembly

The root assembly to start from.

isUsingCache bool

Whether to use cached results from a previous call.

Returns

List<string>

List of all referenced assembly names.

AssemblyWithName(string)

Finds a loaded assembly by name, trying exact name, full name, and partial match.

public static Assembly? AssemblyWithName(string name)

Parameters

name string

The assembly name to search for.

Returns

Assembly

The matching assembly or null.

Clone<T>(T)

Performs a deep copy of an object. Currently not implemented for non-serializable types.

public static T? Clone<T>(T source)

Parameters

source T

The object instance to copy.

Returns

T

The copied object.

Type Parameters

T

The type of object being copied.

CopyObject(object?)

Creates a shallow copy of an object including list fields via reflection.

public static object? CopyObject(object? input)

Parameters

input object

The object to copy.

Returns

object

A copy of the object, or null if input is null.

CopyProperties(object, object)

Copies values of all readable properties from source to target object.

public void CopyProperties(object source, object target)

Parameters

source object

The source object to copy from.

target object

The target object to copy to.

DumpAsObjectDumperNet(object)

Dumps an object using the ObjectDumper.NET library.

public static string DumpAsObjectDumperNet(object instance)

Parameters

instance object

The object to dump.

Returns

string

String representation from ObjectDumper.NET.

DumpAsReflection(object)

Dumps an object using the Reflection provider.

public static string DumpAsReflection(object instance)

Parameters

instance object

The object to dump.

Returns

string

Reflection-based string representation.

DumpAsString(DumpAsStringArgs)

Dumps an object as a string using the specified dump provider. When serializing ISymbol, execution may take unlimited time.

public static string DumpAsString(DumpAsStringArgs args)

Parameters

args DumpAsStringArgs

The dump arguments specifying the object, provider, and options.

Returns

string

String representation of the object.

DumpAsString3(object, DumpAsStringHeaderArgsReflection?)

Dumps an object as a string using default header arguments with delimiter-separated values. Mainly for fast comparing objects. Only OnlyNames can be specified via DumpAsStringHeaderArgsReflection. For more customization, use DumpAsString with DumpAsStringArgs directly.

public static string DumpAsString3(object instance, DumpAsStringHeaderArgsReflection? args = null)

Parameters

instance object

The object to dump.

args DumpAsStringHeaderArgsReflection

Optional header arguments for name filtering.

Returns

string

Delimiter-separated string of object values.

DumpAsString3Dictionary2<TKey, TValue>(string, Dictionary<TKey, List<TValue>>)

Dumps a two-level nested dictionary as a formatted string.

public static string DumpAsString3Dictionary2<TKey, TValue>(string operation, Dictionary<TKey, List<TValue>> grouped) where TKey : notnull

Parameters

operation string

The operation label for the header.

grouped Dictionary<TKey, List<TValue>>

The nested dictionary to dump.

Returns

string

Formatted multi-line string.

Type Parameters

TKey

Type of the dictionary key.

TValue

Type of the list values.

DumpAsString3Dictionary3<TKey1, TKey2, TValue>(string, Dictionary<TKey1, Dictionary<TKey2, List<TValue>>>)

Dumps a three-level nested dictionary as a formatted string.

public static string DumpAsString3Dictionary3<TKey1, TKey2, TValue>(string operation, Dictionary<TKey1, Dictionary<TKey2, List<TValue>>> grouped) where TKey1 : notnull where TKey2 : notnull

Parameters

operation string

The operation label for the header.

grouped Dictionary<TKey1, Dictionary<TKey2, List<TValue>>>

The nested dictionary to dump.

Returns

string

Formatted multi-line string.

Type Parameters

TKey1

Type of the first-level key.

TKey2

Type of the second-level key.

TValue

Type of the list values.

DumpAsXml(object)

Serializes an object to XML string. Returns the exception text if serialization fails.

public static string DumpAsXml(object output)

Parameters

output object

The object to serialize to XML.

Returns

string

XML string representation of the object.

DumpListAsString(DumpAsStringArgs, bool)

Dumps a list of objects as a multi-line string with a header row of field names.

public static string DumpListAsString(DumpAsStringArgs args, bool isRemovingNull = false)

Parameters

args DumpAsStringArgs

Dump arguments containing the list object.

isRemovingNull bool

Whether to remove null entries from the list before dumping.

Returns

string

Formatted string representation of the list.

DumpListAsStringOneLine(string, IList, DumpAsStringHeaderArgsReflection)

Dumps a list of objects as a compact one-line-per-item string with a header.

public static string DumpListAsStringOneLine(string operation, IList list, DumpAsStringHeaderArgsReflection args)

Parameters

operation string

The operation name to display in the header.

list IList

The list of objects to dump.

args DumpAsStringHeaderArgsReflection

Header arguments controlling which fields to include.

Returns

string

Formatted string representation.

ExistsAssemblyNotFullName(string)

Checks whether a named assembly exists in the current domain's referenced assemblies.

public static bool ExistsAssemblyNotFullName(string value)

Parameters

value string

The assembly name to search for (not full name).

Returns

bool

True if the assembly is referenced.

ExistsClass(string)

Checks whether a class with the specified name exists in any loaded assembly.

public static bool ExistsClass(string className)

Parameters

className string

The class name to search for.

Returns

bool

True if a class with the specified name exists.

FullNameOfClassEndsDot(Type)

Gets the full name of a class ending with a dot.

public static string FullNameOfClassEndsDot(Type type)

Parameters

type Type

The type to get the full name for.

Returns

string

Full name followed by a dot.

FullNameOfExecutedCode(MethodBase)

Gets the full name of the currently executing code location from a MethodBase.

public static string FullNameOfExecutedCode(MethodBase method)

Parameters

method MethodBase

The method base to extract info from.

Returns

string

Formatted string "TypeName.MethodName:".

FullNameOfMethod(MethodInfo)

Gets the full name of a method (DeclaringType.FullName + MethodName).

public static string FullNameOfMethod(MethodInfo methodInfo)

Parameters

methodInfo MethodInfo

The method info to get the name from.

Returns

string

Full name string.

FullPathCodeEntity(Type)

Returns the full path of a code entity (Namespace.Name).

public static string FullPathCodeEntity(Type type)

Parameters

type Type

The type to get the full path for.

Returns

string

Full namespace-qualified name.

GetConsts(Type, GetMemberArgs?)

Gets all constant fields defined on the specified type, optionally including non-public constants. Returns FieldInfo objects so the caller can extract Name, Value, etc.

public static List<FieldInfo> GetConsts(Type type, GetMemberArgs? args = null)

Parameters

type Type

The type to inspect.

args GetMemberArgs

Optional arguments controlling public-only filtering.

Returns

List<FieldInfo>

List of constant FieldInfo objects.

GetFields(object)

Gets all public fields of an object. The parameter can be a Type or an instance.

public static List<FieldInfo> GetFields(object instance)

Parameters

instance object

The object or Type to inspect.

Returns

List<FieldInfo>

List of public fields.

GetMethods(Type)

Gets all public static methods defined on the specified type including inherited.

public static List<MethodInfo> GetMethods(Type type)

Parameters

type Type

The type to inspect.

Returns

List<MethodInfo>

List of public static methods.

GetOrSetValue(string, Type, object, IList, bool, Func<object, MemberInfo[], object?, object?>, object?)

Gets or sets a member value by name, using the provided delegate for the actual get/set operation.

public static object? GetOrSetValue(string name, Type type, object instance, IList members, bool isIgnoringCase, Func<object, MemberInfo[], object?, object?> getOrSet, object? value)

Parameters

name string

The member name to find.

type Type

The type containing the member.

instance object

The object instance.

members IList

The list of members to search in.

isIgnoringCase bool

Whether to ignore case when matching names.

getOrSet Func<object, MemberInfo[], object, object>

The delegate to perform the actual get or set operation.

value object

The value to set (used only for set operations).

Returns

object

The member value for get operations, or null.

GetPropertyNames(Type)

Gets all property names declared on the specified type (not inherited), regardless of access modifier.

public static List<string> GetPropertyNames(Type type)

Parameters

type Type

The type to inspect.

Returns

List<string>

List of property names.

GetTypesInAssembly(Assembly, string)

Gets all types in the assembly whose names contain the specified string. Better than loading assemblies directly from the running process is using Assembly.LoadFrom. If you encounter "Could not load file or assembly System.Reflection.Metadata", add System.Reflection.Metadata to both the calling and target projects.

public static IList<Type> GetTypesInAssembly(Assembly assembly, string contains)

Parameters

assembly Assembly

The assembly to search.

contains string

The substring to search for in type names.

Returns

IList<Type>

List of matching types.

GetTypesInNamespace(Assembly, string)

Gets all types defined in the specified namespace within the given assembly.

public static IList<Type> GetTypesInNamespace(Assembly assembly, string nameSpace)

Parameters

assembly Assembly

The assembly to search.

nameSpace string

The namespace to filter by.

Returns

IList<Type>

List of types in the specified namespace.

GetValue(MemberInfo, bool, List<string>, List<string>, object, List<string>, bool)

Extracts the value of a member (property or field) and adds it to the values list, applying name filters.

public static void GetValue(MemberInfo descriptor, bool isAllNegated, List<string> onlyNames, List<string> filterNames, object instance, List<string> values, bool isOnlyValues)

Parameters

descriptor MemberInfo

The member to extract the value from.

isAllNegated bool

Whether all filter names are negation filters.

onlyNames List<string>

The original name filter list.

filterNames List<string>

Working copy of the filter list.

instance object

The object instance.

values List<string>

The list to add extracted values to.

isOnlyValues bool

When true, adds only the value without the name prefix.

GetValue(string, Type, object, IList, bool, object?)

Gets a member value by name from an object instance, searching in the provided member list.

public static object? GetValue(string name, Type type, object instance, IList members, bool isIgnoringCase, object? value)

Parameters

name string

The member name to find.

type Type

The type containing the member.

instance object

The object instance.

members IList

The list of members to search in.

isIgnoringCase bool

Whether to ignore case when matching names.

value object

Unused parameter (reserved for set operations).

Returns

object

The member value or null if not found.

GetValueOfField(string, Type, object, bool)

Gets the value of a field by name from an object instance.

public static object? GetValueOfField(string name, Type type, object instance, bool isIgnoringCase)

Parameters

name string

The field name.

type Type

The type containing the field.

instance object

The object instance (null for static fields).

isIgnoringCase bool

Whether to ignore case when matching names.

Returns

object

The field value.

GetValueOfProperty(string, Type, object, bool)

Gets the value of a property by name from an object instance.

public static object? GetValueOfProperty(string name, Type type, object instance, bool isIgnoringCase)

Parameters

name string

The property name.

type Type

The type containing the property.

instance object

The object instance.

isIgnoringCase bool

Whether to ignore case when matching names.

Returns

object

The property value.

GetValueOfPropertyOrField(object, string)

Gets the value of a property or field by name from an object instance.

public static object? GetValueOfPropertyOrField(object instance, string name)

Parameters

instance object

The object instance to inspect.

name string

The name of the property or field.

Returns

object

The value of the property or field.

GetValuesOfConsts(Type)

Gets string values of all constants defined on the specified type.

public static List<string> GetValuesOfConsts(Type type)

Parameters

type Type

The type to inspect for constants.

Returns

List<string>

Trimmed string values of the constants.

GetValuesOfConsts(Type, params string[])

Gets constant values as a dictionary of name-value pairs, optionally filtered by name.

public static Dictionary<string, string> GetValuesOfConsts(Type type, params string[] onlyNames)

Parameters

type Type

The type to inspect for constants.

onlyNames string[]

Optional filter for specific constant names.

Returns

Dictionary<string, string>

Dictionary mapping constant names to their string values.

GetValuesOfField(object, IList<string>, bool)

Gets string values of all fields from an object with control over value-only output.

public static List<string> GetValuesOfField(object instance, IList<string> onlyNames, bool isOnlyValues)

Parameters

instance object

The object to inspect.

onlyNames IList<string>

Filter for specific field names.

isOnlyValues bool

When true, returns only values without field names.

Returns

List<string>

List of field values as strings.

GetValuesOfField(object, params string[])

Gets string values of all fields from an object, optionally filtered by name.

public static List<string> GetValuesOfField(object instance, params string[] onlyNames)

Parameters

instance object

The object to inspect.

onlyNames string[]

Optional filter for specific field names.

Returns

List<string>

List of field values as strings.

GetValuesOfProperty(object, params string[])

Gets string values of all properties from an object using reflection. For more complex .NET objects, use GetValuesOfProperty2 instead.

public static List<string> GetValuesOfProperty(object instance, params string[] onlyNames)

Parameters

instance object

The object to inspect.

onlyNames string[]

Optional filter for specific property names.

Returns

List<string>

List of property values as strings.

GetValuesOfProperty2(object, List<string>, bool)

Gets property values of an object, falling back to fields if no properties exist. Supports negation filters with "!" prefix in onlyNames.

public static List<string> GetValuesOfProperty2(object instance, List<string> onlyNames, bool isOnlyValues)

Parameters

instance object

The object to inspect.

onlyNames List<string>

Filter for specific property names. Prefix with "!" to exclude.

isOnlyValues bool

When true, returns only values without property names.

Returns

List<string>

List of property/field values as strings.

GetValuesOfPropertyOrField(object, params string[])

Gets string values of all properties and fields from an object, optionally filtered by name.

public static List<string> GetValuesOfPropertyOrField(object instance, params string[] onlyNames)

Parameters

instance object

The object to inspect.

onlyNames string[]

Optional filter for specific property/field names.

Returns

List<string>

Combined list of property and field values as strings.

IsOrIsDeriveFromBaseClass(Type?, Type, bool)

Checks whether the child type is or derives from the parent type, including interface checks.

public static bool IsOrIsDeriveFromBaseClass(Type? children, Type parent, bool isAllowingString = true)

Parameters

children Type

The child type to check.

parent Type

The parent type to check against.

isAllowingString bool

Whether to allow string type as a valid child.

Returns

bool

True if children is or derives from parent.

PrintPublicPropertiesRecursively(StringBuilder, Type?, string)

Recursively prints the public properties of a type and its nested complex types. This method was useful in usysu where StackOverflowException occurred.

public static void PrintPublicPropertiesRecursively(StringBuilder stringBuilder, Type? type, string indent = "  ")

Parameters

stringBuilder StringBuilder

The StringBuilder to append output to.

type Type

The type to inspect.

indent string

The indentation prefix for nested levels.

SetValue(string, Type, object, IList, bool, object?)

Sets a member value by name, searching in the provided member list.

public static object? SetValue(string name, Type type, object instance, IList members, bool isIgnoringCase, object? value)

Parameters

name string

The member name.

type Type

The type containing the member.

instance object

The object instance.

members IList

The list of members to search.

isIgnoringCase bool

Whether to ignore case when matching names.

value object

The value to set.

Returns

object

Always null.

SetValueOfProperty(string, Type, object, bool, object?)

Sets the value of a property by name on an object instance.

public static object? SetValueOfProperty(string name, Type type, object instance, bool isIgnoringCase, object? value)

Parameters

name string

The property name.

type Type

The type containing the property.

instance object

The object instance.

isIgnoringCase bool

Whether to ignore case when matching names.

value object

The value to set.

Returns

object

Always null (set operation has no return value).