Show / Hide Table of Contents

Class ReflectionHelper

Helper class for using reflection in combination with Spice#.

Inheritance
System.Object
ReflectionHelper
Inherited Members
System.Object.ToString()
System.Object.Equals(System.Object)
System.Object.Equals(System.Object, System.Object)
System.Object.ReferenceEquals(System.Object, System.Object)
System.Object.GetHashCode()
System.Object.GetType()
System.Object.MemberwiseClone()
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 destination does not have the same type as source.

System.ArgumentNullException

Thrown if source or destination is null.

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 null if the property doesn't exist.

Type Parameters
Name Description
P

The parameter type.

Exceptions
Type Condition
System.ArgumentNullException

Thrown if source or name is null.

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 field is null.

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 member is null.

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 method is null.

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 property is null.

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 null if the parameter doesn't exist.

Type Parameters
Name Description
P

The parameter type.

Exceptions
Type Condition
System.ArgumentNullException

Thrown if source or name is null.

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 field is null.

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 member is null.

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 method is null.

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 property is null.

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 source or name is null.

ParameterNotFoundException

Thrown if the the parameter with the specified name could not be found.

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 type is null.

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 source or name is null.

ParameterNotFoundException

Thrown if the the parameter with the specified name could not be found.

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

true if the parameter is returned; otherwise false.

Type Parameters
Name Description
P

The parameter type.

Exceptions
Type Condition
System.ArgumentNullException

Thrown if source or name is null.

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

true if the parameter was set; otherwise false.

Type Parameters
Name Description
P

The parameter type.

Exceptions
Type Condition
System.ArgumentNullException

Thrown if source or name is null.

Back to top Generated by DocFX