Class ReflectionHelper
Helper class for using reflection in combination with Spice#.
Inheritance
Inherited Members
Namespace: SpiceSharp
Assembly: SpiceSharp.dll
Syntax
public static class ReflectionHelper
Properties
Comparer
Gets or sets the default comparer used when creating a parameter mapping.
Declaration
public static IEqualityComparer<string> Comparer { get; set; }
Property Value
| Type | Description |
|---|---|
| System.Collections.Generic.IEqualityComparer<System.String> | The default comparer used. |
Methods
CopyPropertiesAndFields(Object, Object)
Copies all properties and fields from a source object to a destination object.
Declaration
public static void CopyPropertiesAndFields(object source, object destination)
Parameters
| Type | Name | Description |
|---|---|---|
| System.Object | source | The source object. |
| System.Object | destination | The destination object |
Remarks
This method heavily uses reflection to find valid properties and methods. It supports properties and fields of types System.Double, System.Int32, System.String, System.Boolean and ICloneable.
Exceptions
| Type | Condition |
|---|---|
| System.ArgumentException | Thrown if |
| System.ArgumentNullException | Thrown if |
CreateGetter<P>(Object, String)
Creates a getter for the parameter with the specified name.
Declaration
public static Func<P> CreateGetter<P>(object source, string name)
Parameters
| Type | Name | Description |
|---|---|---|
| System.Object | source | The source object. |
| System.String | name | The name of the parameter. |
Returns
| Type | Description |
|---|---|
| System.Func<P> | The function that can return the value of the parameter, or |
Type Parameters
| Name | Description |
|---|---|
| P | The parameter type. |
Exceptions
| Type | Condition |
|---|---|
| System.ArgumentNullException | Thrown if |
CreateGetterForField<T>(Object, FieldInfo)
Creates a getter for a field.
Declaration
public static Func<T> CreateGetterForField<T>(object source, FieldInfo field)
Parameters
| Type | Name | Description |
|---|---|---|
| System.Object | source | The source object. |
| System.Reflection.FieldInfo | field | The field reflection information. |
Returns
| Type | Description |
|---|---|
| System.Func<T> | A function that gets the field value for this instance. |
Type Parameters
| Name | Description |
|---|---|
| T | The base value type. |
Exceptions
| Type | Condition |
|---|---|
| System.ArgumentNullException | Thrown if |
CreateGetterForMember<T>(Object, MemberInfo)
Creates a getter for a member.
Declaration
public static Func<T> CreateGetterForMember<T>(object source, MemberInfo member)
Parameters
| Type | Name | Description |
|---|---|---|
| System.Object | source | The source object. |
| System.Reflection.MemberInfo | member | The member reflection information. |
Returns
| Type | Description |
|---|---|
| System.Func<T> | A function that gets the member of this object. |
Type Parameters
| Name | Description |
|---|---|
| T | The base value type. |
Exceptions
| Type | Condition |
|---|---|
| System.ArgumentNullException | Thrown if |
CreateGetterForMethod<T>(Object, MethodInfo)
Creates a getter for a method.
Declaration
public static Func<T> CreateGetterForMethod<T>(object source, MethodInfo method)
Parameters
| Type | Name | Description |
|---|---|---|
| System.Object | source | The source object. |
| System.Reflection.MethodInfo | method | The method reflection information. |
Returns
| Type | Description |
|---|---|
| System.Func<T> | A function that calls the method for this instance. |
Type Parameters
| Name | Description |
|---|---|
| T | The base value type. |
Exceptions
| Type | Condition |
|---|---|
| System.ArgumentNullException | Thrown if |
CreateGetterForProperty<T>(Object, PropertyInfo)
Creates a getter for a property.
Declaration
public static Func<T> CreateGetterForProperty<T>(object source, PropertyInfo property)
Parameters
| Type | Name | Description |
|---|---|---|
| System.Object | source | The source object. |
| System.Reflection.PropertyInfo | property | The property reflection information. |
Returns
| Type | Description |
|---|---|
| System.Func<T> | A function that gets the property value for this instance. |
Type Parameters
| Name | Description |
|---|---|
| T | The base value type. |
Exceptions
| Type | Condition |
|---|---|
| System.ArgumentNullException | Thrown if |
CreateSetter<P>(Object, String)
Creates a setter for the parameter with the specified name.
Declaration
public static Action<P> CreateSetter<P>(object source, string name)
Parameters
| Type | Name | Description |
|---|---|---|
| System.Object | source | The source object. |
| System.String | name | The name of the parameter. |
Returns
| Type | Description |
|---|---|
| System.Action<P> | The action that can set the value of the parameter, or |
Type Parameters
| Name | Description |
|---|---|
| P | The parameter type. |
Exceptions
| Type | Condition |
|---|---|
| System.ArgumentNullException | Thrown if |
CreateSetterForField<T>(Object, FieldInfo)
Creates a setter for a field.
Declaration
public static Action<T> CreateSetterForField<T>(object source, FieldInfo field)
Parameters
| Type | Name | Description |
|---|---|---|
| System.Object | source | The source object. |
| System.Reflection.FieldInfo | field | The field reflection information. |
Returns
| Type | Description |
|---|---|
| System.Action<T> | An action that sets the field value for this instance. |
Type Parameters
| Name | Description |
|---|---|
| T | The base value type. |
Exceptions
| Type | Condition |
|---|---|
| System.ArgumentNullException | Thrown if |
CreateSetterForMember<T>(Object, MemberInfo)
Creates a setter for a member.
Declaration
public static Action<T> CreateSetterForMember<T>(object source, MemberInfo member)
Parameters
| Type | Name | Description |
|---|---|---|
| System.Object | source | The source object. |
| System.Reflection.MemberInfo | member | The member reflection information. |
Returns
| Type | Description |
|---|---|
| System.Action<T> | An action that sets the member of this object. |
Type Parameters
| Name | Description |
|---|---|
| T | The base value type. |
Exceptions
| Type | Condition |
|---|---|
| System.ArgumentNullException | Thrown if |
CreateSetterForMethod<T>(Object, MethodInfo)
Creates a setter for a method.
Declaration
public static Action<T> CreateSetterForMethod<T>(object source, MethodInfo method)
Parameters
| Type | Name | Description |
|---|---|---|
| System.Object | source | The source object. |
| System.Reflection.MethodInfo | method | The method reflection information. |
Returns
| Type | Description |
|---|---|
| System.Action<T> | An action that calls the method for this instance. |
Type Parameters
| Name | Description |
|---|---|
| T | The base value type. |
Exceptions
| Type | Condition |
|---|---|
| System.ArgumentNullException | Thrown if |
CreateSetterForProperty<T>(Object, PropertyInfo)
Creates a setter for a property.
Declaration
public static Action<T> CreateSetterForProperty<T>(object source, PropertyInfo property)
Parameters
| Type | Name | Description |
|---|---|---|
| System.Object | source | The source object. |
| System.Reflection.PropertyInfo | property | The property reflection information. |
Returns
| Type | Description |
|---|---|
| System.Action<T> | An action that sets the property value for this instance. |
Type Parameters
| Name | Description |
|---|---|
| T | The base value type. |
Exceptions
| Type | Condition |
|---|---|
| System.ArgumentNullException | Thrown if |
Get<P>(Object, String)
Gets the parameter.
Declaration
public static P Get<P>(object source, string name)
Parameters
| Type | Name | Description |
|---|---|---|
| System.Object | source | The source object. |
| System.String | name | The name of the parameter. |
Returns
| Type | Description |
|---|---|
| P | The value of the parameter. |
Type Parameters
| Name | Description |
|---|---|
| P | The parameter type. |
Exceptions
| Type | Condition |
|---|---|
| System.ArgumentNullException | Thrown if |
| ParameterNotFoundException | Thrown if the the parameter with the specified |
GetParameterMap(Type)
Gets the parameter map of the specified type.
Declaration
public static ParameterMap GetParameterMap(Type type)
Parameters
| Type | Name | Description |
|---|---|---|
| System.Type | type | The type. |
Returns
| Type | Description |
|---|---|
| ParameterMap | The parameter map. |
Exceptions
| Type | Condition |
|---|---|
| System.ArgumentNullException | Thrown if |
Set<P>(Object, String, P)
Sets the parameter.
Declaration
public static void Set<P>(object source, string name, P value)
Parameters
| Type | Name | Description |
|---|---|---|
| System.Object | source | The source. |
| System.String | name | The name. |
| P | value | The value. |
Type Parameters
| Name | Description |
|---|---|
| P | The parameter type. |
Exceptions
| Type | Condition |
|---|---|
| System.ArgumentNullException | Thrown if |
| ParameterNotFoundException | Thrown if the the parameter with the specified |
TryGet<P>(Object, String, out P)
Tries to get the parameter with the specified name.
Declaration
public static bool TryGet<P>(object source, string name, out P value)
Parameters
| Type | Name | Description |
|---|---|---|
| System.Object | source | The source object. |
| System.String | name | The name of the parameter. |
| P | value | The value. |
Returns
| Type | Description |
|---|---|
| System.Boolean |
|
Type Parameters
| Name | Description |
|---|---|
| P | The parameter type. |
Exceptions
| Type | Condition |
|---|---|
| System.ArgumentNullException | Thrown if |
TrySet<P>(Object, String, P)
Tries to set the parameter with the specified name.
Declaration
public static bool TrySet<P>(object source, string name, P value)
Parameters
| Type | Name | Description |
|---|---|---|
| System.Object | source | The source object. |
| System.String | name | The name of the parameter. |
| P | value | The value. |
Returns
| Type | Description |
|---|---|
| System.Boolean |
|
Type Parameters
| Name | Description |
|---|---|
| P | The parameter type. |
Exceptions
| Type | Condition |
|---|---|
| System.ArgumentNullException | Thrown if |