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
entryAssemblyAssemblyThe root assembly to start from.
isUsingCacheboolWhether to use cached results from a previous call.
Returns
AssemblyWithName(string)
Finds a loaded assembly by name, trying exact name, full name, and partial match.
public static Assembly? AssemblyWithName(string name)
Parameters
namestringThe 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
sourceTThe object instance to copy.
Returns
- T
The copied object.
Type Parameters
TThe 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
inputobjectThe 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
DumpAsObjectDumperNet(object)
Dumps an object using the ObjectDumper.NET library.
public static string DumpAsObjectDumperNet(object instance)
Parameters
instanceobjectThe 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
instanceobjectThe 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
argsDumpAsStringArgsThe 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
instanceobjectThe object to dump.
argsDumpAsStringHeaderArgsReflectionOptional 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
operationstringThe operation label for the header.
groupedDictionary<TKey, List<TValue>>The nested dictionary to dump.
Returns
- string
Formatted multi-line string.
Type Parameters
TKeyType of the dictionary key.
TValueType 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
operationstringThe operation label for the header.
groupedDictionary<TKey1, Dictionary<TKey2, List<TValue>>>The nested dictionary to dump.
Returns
- string
Formatted multi-line string.
Type Parameters
TKey1Type of the first-level key.
TKey2Type of the second-level key.
TValueType 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
outputobjectThe 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
argsDumpAsStringArgsDump arguments containing the list object.
isRemovingNullboolWhether 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
operationstringThe operation name to display in the header.
listIListThe list of objects to dump.
argsDumpAsStringHeaderArgsReflectionHeader 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
valuestringThe 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
classNamestringThe 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
typeTypeThe 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
methodMethodBaseThe 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
methodInfoMethodInfoThe 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
typeTypeThe 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
typeTypeThe type to inspect.
argsGetMemberArgsOptional arguments controlling public-only filtering.
Returns
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
instanceobjectThe object or Type to inspect.
Returns
GetMethods(Type)
Gets all public static methods defined on the specified type including inherited.
public static List<MethodInfo> GetMethods(Type type)
Parameters
typeTypeThe 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
namestringThe member name to find.
typeTypeThe type containing the member.
instanceobjectThe object instance.
membersIListThe list of members to search in.
isIgnoringCaseboolWhether to ignore case when matching names.
getOrSetFunc<object, MemberInfo[], object, object>The delegate to perform the actual get or set operation.
valueobjectThe 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
typeTypeThe type to inspect.
Returns
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
assemblyAssemblyThe assembly to search.
containsstringThe substring to search for in type names.
Returns
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
Returns
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
descriptorMemberInfoThe member to extract the value from.
isAllNegatedboolWhether all filter names are negation filters.
onlyNamesList<string>The original name filter list.
filterNamesList<string>Working copy of the filter list.
instanceobjectThe object instance.
valuesList<string>The list to add extracted values to.
isOnlyValuesboolWhen 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
namestringThe member name to find.
typeTypeThe type containing the member.
instanceobjectThe object instance.
membersIListThe list of members to search in.
isIgnoringCaseboolWhether to ignore case when matching names.
valueobjectUnused 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
namestringThe field name.
typeTypeThe type containing the field.
instanceobjectThe object instance (null for static fields).
isIgnoringCaseboolWhether 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
namestringThe property name.
typeTypeThe type containing the property.
instanceobjectThe object instance.
isIgnoringCaseboolWhether 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
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
typeTypeThe type to inspect for constants.
Returns
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
typeTypeThe type to inspect for constants.
onlyNamesstring[]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
instanceobjectThe object to inspect.
onlyNamesIList<string>Filter for specific field names.
isOnlyValuesboolWhen true, returns only values without field names.
Returns
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
Returns
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
instanceobjectThe object to inspect.
onlyNamesstring[]Optional filter for specific property names.
Returns
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
instanceobjectThe object to inspect.
onlyNamesList<string>Filter for specific property names. Prefix with "!" to exclude.
isOnlyValuesboolWhen true, returns only values without property names.
Returns
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
instanceobjectThe object to inspect.
onlyNamesstring[]Optional filter for specific property/field names.
Returns
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
childrenTypeThe child type to check.
parentTypeThe parent type to check against.
isAllowingStringboolWhether 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
stringBuilderStringBuilderThe StringBuilder to append output to.
typeTypeThe type to inspect.
indentstringThe 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
namestringThe member name.
typeTypeThe type containing the member.
instanceobjectThe object instance.
membersIListThe list of members to search.
isIgnoringCaseboolWhether to ignore case when matching names.
valueobjectThe 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
namestringThe property name.
typeTypeThe type containing the property.
instanceobjectThe object instance.
isIgnoringCaseboolWhether to ignore case when matching names.
valueobjectThe value to set.
Returns
- object
Always null (set operation has no return value).