I want to learn dynamic method and its practical example using c#.
Is there any relation between dynamic method and Reflection?
Please help me.
We are using Dynamic methods for speed up Reflection.
Here is code of our reflection optimizer. it is only 10% slower than direct call and 2000 times faster that reflection call
public class ReflectionEmitPropertyAccessor
{
private readonly bool canRead;
private readonly bool canWrite;
private IPropertyAccessor emittedPropertyAccessor;
private readonly string propertyName;
private readonly Type propertyType;
private readonly Type targetType;
private Dictionary<Type,OpCode> typeOpCodes;
public ReflectionEmitPropertyAccessor(Type targetType, string property)
{
this.targetType = targetType;
propertyName = property;
var propertyInfo = targetType.GetProperty(property);
if (propertyInfo == null)
{
throw new ReflectionOptimizerException(string.Format("Property \"{0}\" is not found in type "+ "{1}.", property, targetType));
}
canRead = propertyInfo.CanRead;
canWrite = propertyInfo.CanWrite;
propertyType = propertyInfo.PropertyType;
}
public bool CanRead
{
get { return canRead; }
}
public bool CanWrite
{
get { return canWrite; }
}
public Type TargetType
{
get { return targetType; }
}
public Type PropertyType
{
get { return propertyType; }
}
#region IPropertyAccessor Members
public object Get(object target)
{
if (canRead)
{
if (emittedPropertyAccessor == null)
{
Init();
}
if (emittedPropertyAccessor != null) return emittedPropertyAccessor.Get(target);
}
else
{
throw new ReflectionOptimizerException(string.Format("У свойства \"{0}\" нет метода get.", propertyName));
}
throw new ReflectionOptimizerException("Fail initialize of " + GetType().FullName);
}
public void Set(object target, object value)
{
if (canWrite)
{
if (emittedPropertyAccessor == null)
{
Init();
}
if (emittedPropertyAccessor != null) emittedPropertyAccessor.Set(target, value);
}
else
{
throw new ReflectionOptimizerException(string.Format("Property \"{0}\" does not have method set.", propertyName));
}
throw new ReflectionOptimizerException("Fail initialize of " + GetType().FullName);
}
#endregion
private void Init()
{
InitTypeOpCodes();
var assembly = EmitAssembly();
emittedPropertyAccessor = assembly.CreateInstance("Property") as IPropertyAccessor;
if (emittedPropertyAccessor == null)
{
throw new ReflectionOptimizerException("Shit happense in PropertyAccessor.");
}
}
private void InitTypeOpCodes()
{
typeOpCodes = new Dictionary<Type, OpCode>
{
{typeof (sbyte), OpCodes.Ldind_I1},
{typeof (byte), OpCodes.Ldind_U1},
{typeof (char), OpCodes.Ldind_U2},
{typeof (short), OpCodes.Ldind_I2},
{typeof (ushort), OpCodes.Ldind_U2},
{typeof (int), OpCodes.Ldind_I4},
{typeof (uint), OpCodes.Ldind_U4},
{typeof (long), OpCodes.Ldind_I8},
{typeof (ulong), OpCodes.Ldind_I8},
{typeof (bool), OpCodes.Ldind_I1},
{typeof (double), OpCodes.Ldind_R8},
{typeof (float), OpCodes.Ldind_R4}
};
}
private Assembly EmitAssembly()
{
var assemblyName = new AssemblyName {Name = "PropertyAccessorAssembly"};
var newAssembly = Thread.GetDomain().DefineDynamicAssembly(assemblyName, AssemblyBuilderAccess.Run);
var newModule = newAssembly.DefineDynamicModule("Module");
var dynamicType = newModule.DefineType("Property", TypeAttributes.Public);
dynamicType.AddInterfaceImplementation(typeof(IPropertyAccessor));
dynamicType.DefineDefaultConstructor(MethodAttributes.Public);
var getParamTypes = new[] { typeof(object) };
var getReturnType = typeof(object);
var getMethod = dynamicType.DefineMethod("Get",
MethodAttributes.Public | MethodAttributes.Virtual,
getReturnType,
getParamTypes);
var getIL = getMethod.GetILGenerator();
var targetGetMethod = targetType.GetMethod("get_" + propertyName);
if (targetGetMethod != null)
{
getIL.DeclareLocal(typeof(object));
getIL.Emit(OpCodes.Ldarg_1); //Load the first argument
getIL.Emit(OpCodes.Castclass, targetType); //Cast to the source type
getIL.EmitCall(OpCodes.Call, targetGetMethod, null); //Get the property value
if (targetGetMethod.ReturnType.IsValueType)
{
getIL.Emit(OpCodes.Box, targetGetMethod.ReturnType); //Box
}
getIL.Emit(OpCodes.Stloc_0); //Store it
getIL.Emit(OpCodes.Ldloc_0);
}
else
{
getIL.ThrowException(typeof(MissingMethodException));
}
getIL.Emit(OpCodes.Ret);
var setParamTypes = new[] { typeof(object), typeof(object) };
const Type setReturnType = null;
var setMethod = dynamicType.DefineMethod("Set",
MethodAttributes.Public | MethodAttributes.Virtual,
setReturnType,
setParamTypes);
var setIL = setMethod.GetILGenerator();
var targetSetMethod = targetType.GetMethod("set_" + propertyName);
if (targetSetMethod != null)
{
Type paramType = targetSetMethod.GetParameters()[0].ParameterType;
setIL.DeclareLocal(paramType);
setIL.Emit(OpCodes.Ldarg_1); //Load the first argument //(target object)
setIL.Emit(OpCodes.Castclass, targetType); //Cast to the source type
setIL.Emit(OpCodes.Ldarg_2); //Load the second argument
//(value object)
if (paramType.IsValueType)
{
setIL.Emit(OpCodes.Unbox, paramType); //Unbox it
if (typeOpCodes.ContainsKey(paramType)) //and load
{
var load = typeOpCodes[paramType];
setIL.Emit(load);
}
else
{
setIL.Emit(OpCodes.Ldobj, paramType);
}
}
else
{
setIL.Emit(OpCodes.Castclass, paramType); //Cast class
}
setIL.EmitCall(OpCodes.Callvirt,targetSetMethod, null); //Set the property value
}
else
{
setIL.ThrowException(typeof(MissingMethodException));
}
setIL.Emit(OpCodes.Ret);
// Load the type
dynamicType.CreateType();
return newAssembly;
}
}
implementation is aggregated from different sources and main is this CodeProject article.
You can create a method via DynamicMethod class.
DynamicMethod squareIt = new DynamicMethod(
"SquareIt",
typeof(long),
methodArgs,
typeof(Example).Module);
ILGenerator il = squareIt.GetILGenerator();
il.Emit(OpCodes.Ldarg_0);
il.Emit(OpCodes.Conv_I8);
il.Emit(OpCodes.Dup);
il.Emit(OpCodes.Mul);
il.Emit(OpCodes.Ret);
Fully commented example on MSDN
I should note that the development using this method is very slow and not very useful.
Related
I'm trying to retrieve a value from the following struct.
public struct MyType
{
public string Key { get; set; }
public string Value { get; set; }
}
using the following code.
var mem = new MemberAccessor(typeof(MyType), "Value");
var r = new MyType {Key = "One", Value = "Two"};
object s = mem.Get(r);
Console.WriteLine(s); //Should be Two but is One
And it works, but it gives me the value of "Key" not "Value". If I try to get the value of "Key" then it gives me a "Cannot read from memory" error.
The following is the creation of the Get delegate used in the above code. This only appears to be a problem with a struct.
public class MemberAccessor
{
private readonly Type _targetType;
private readonly Type _memberType;
private readonly MemberInfo _member;
private static readonly Hashtable _mTypeHash = new Hashtable
{
[typeof(sbyte)] = OpCodes.Ldind_I1,
[typeof(byte)] = OpCodes.Ldind_U1,
[typeof(char)] = OpCodes.Ldind_U2,
[typeof(short)] = OpCodes.Ldind_I2,
[typeof(ushort)] = OpCodes.Ldind_U2,
[typeof(int)] = OpCodes.Ldind_I4,
[typeof(uint)] = OpCodes.Ldind_U4,
[typeof(long)] = OpCodes.Ldind_I8,
[typeof(ulong)] = OpCodes.Ldind_I8,
[typeof(bool)] = OpCodes.Ldind_I1,
[typeof(double)] = OpCodes.Ldind_R8,
[typeof(float)] = OpCodes.Ldind_R4
};
public static Type GetMemberInfoType(MemberInfo member)
{
Type type;
if (member is FieldInfo)
type = ((FieldInfo)member).FieldType;
else if (member is PropertyInfo)
type = ((PropertyInfo)member).PropertyType;
else if (member == null)
type = typeof(object);
else
throw new NotSupportedException();
return type;
}
/// <summary>
/// Creates a new property accessor.
/// </summary>
/// <param name="targetType">Target object type.</param>
/// <param name="memberName">Property name.</param>
public MemberAccessor(Type targetType, string memberName)
{
_targetType = targetType;
MemberInfo memberInfo = (targetType).GetProperties().First(x => x.Name == memberName);
if (memberInfo == null)
{
throw new Exception(string.Format("Property \"{0}\" does not exist for type " + "{1}.", memberName, targetType));
}
var canRead = IsField(memberInfo) || ((PropertyInfo)memberInfo).CanRead;
var canWrite = IsField(memberInfo) || ((PropertyInfo)memberInfo).CanWrite;
// roslyn automatically implemented properties, in particular for get-only properties: <{Name}>k__BackingField;
if (!canWrite)
{
var backingFieldName = $"<{memberName}>k__BackingField";
var backingFieldMemberInfo = targetType.GetFields(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance).FirstOrDefault(x => x.Name == backingFieldName);
if (backingFieldMemberInfo != null)
{
memberInfo = backingFieldMemberInfo;
canWrite = true;
}
}
_memberType = GetMemberInfoType(memberInfo);
_member = memberInfo;
if (canWrite)
{
SetDelegate = GetSetDelegate();
}
if (canRead)
{
GetDelegate = GetGetDelegate();
}
}
private Func<object, object> GetDelegate = null;
private Action<object, object> SetDelegate = null;
/// <summary>
/// Sets the property for the specified target.
/// </summary>
/// <param name="target">Target object.</param>
/// <param name="value">Value to set.</param>
public void Set(object target, object value)
{
SetDelegate?.Invoke(target, value);
}
public object Get(object target)
{
return GetDelegate?.Invoke(target);
}
private Action<object, object> GetSetDelegate()
{
Type[] setParamTypes = new Type[] { typeof(object), typeof(object) };
Type setReturnType = null;
var owner = _targetType.GetTypeInfo().IsAbstract || _targetType.GetTypeInfo().IsInterface ? null : _targetType;
var setMethod = owner != null
? new DynamicMethod(Guid.NewGuid().ToString(), setReturnType, setParamTypes, owner, true)
: new DynamicMethod(Guid.NewGuid().ToString(), setReturnType, setParamTypes, true);
// From the method, get an ILGenerator. This is used to
// emit the IL that we want.
//
ILGenerator setIL = setMethod.GetILGenerator();
//
// Emit the IL.
//
Type paramType = _memberType;
setIL.Emit(OpCodes.Ldarg_0); //Load the first argument
//(target object)
//Cast to the source type
setIL.Emit(OpCodes.Castclass, this._targetType);
setIL.Emit(OpCodes.Ldarg_1); //Load the second argument
//(value object)
if (paramType.GetTypeInfo().IsValueType)
{
setIL.Emit(OpCodes.Unbox, paramType); //Unbox it
if (_mTypeHash[paramType] != null) //and load
{
OpCode load = (OpCode)_mTypeHash[paramType];
setIL.Emit(load);
}
else
{
setIL.Emit(OpCodes.Ldobj, paramType);
}
}
else
{
setIL.Emit(OpCodes.Castclass, paramType); //Cast class
}
if (IsField(_member))
{
setIL.Emit(OpCodes.Stfld, (FieldInfo)_member);
}
else
{
MethodInfo targetSetMethod = GetSetMethodOnDeclaringType(((PropertyInfo)this._member));
if (targetSetMethod != null)
{
setIL.Emit(OpCodes.Callvirt, targetSetMethod);
}
else
{
setIL.ThrowException(typeof(MissingMethodException));
}
}
setIL.Emit(OpCodes.Ret);
var del = setMethod.CreateDelegate(Expression.GetActionType(setParamTypes));
return del as Action<object, object>;
}
public static bool IsField(MemberInfo member)
{
return member is FieldInfo;
}
public static MethodInfo GetSetMethodOnDeclaringType(PropertyInfo propertyInfo)
{
var methodInfo = propertyInfo.GetSetMethod(true);
return methodInfo ?? propertyInfo
.DeclaringType
.GetProperty(propertyInfo.Name)
.GetSetMethod(true);
}
private Func<object, object> GetGetDelegate()
{
Type[] setParamTypes = new[] { typeof(object) };
Type setReturnType = typeof(object);
Type owner = _targetType.GetTypeInfo().IsAbstract || _targetType.GetTypeInfo().IsInterface ? null : _targetType;
var getMethod = owner != null
? new DynamicMethod(Guid.NewGuid().ToString(), setReturnType, setParamTypes, owner, true)
: new DynamicMethod(Guid.NewGuid().ToString(), setReturnType, setParamTypes, true);
// From the method, get an ILGenerator. This is used to
// emit the IL that we want.
ILGenerator getIL = getMethod.GetILGenerator();
getIL.DeclareLocal(typeof(object));
getIL.Emit(OpCodes.Ldarg_0); //Load the first argument
//(target object)
//Cast to the source type
getIL.Emit(OpCodes.Castclass, this._targetType);
//Get the property value
if (IsField(_member))
{
getIL.Emit(OpCodes.Ldfld, (FieldInfo)_member);
if (_memberType.GetTypeInfo().IsValueType)
{
getIL.Emit(OpCodes.Box, _memberType);
}
}
else
{
var targetGetMethod = ((PropertyInfo)_member).GetGetMethod();
getIL.Emit(OpCodes.Callvirt, targetGetMethod);
if (targetGetMethod.ReturnType.GetTypeInfo().IsValueType)
{
getIL.Emit(OpCodes.Box, targetGetMethod.ReturnType);
}
}
getIL.Emit(OpCodes.Ret);
var del = getMethod.CreateDelegate(Expression.GetFuncType(setParamTypes.Concat(new[] { setReturnType }).ToArray()));
return del as Func<object, object>;
}
}
Problem in the IL code that you generate for GetDelegate. You should unbox struct from object type parameter before calling GetMethod of it's property, like this:
if (_targetType.IsValueType)
getIL.Emit(OpCodes.Unbox, _targetType);
else
getIL.Emit(OpCodes.Castclass, this._targetType);
Small side notes about GetGetDelegate method:
For struct types you can use Call instead Callvirt because it's faster and struct can't have virtual properties.
I was't able to understand why do you need declare local variable with this instruction getIL.DeclareLocal(typeof(object)) and I believe you can safely remove this line.
Just in case here version of GetGetDelegate method that worked for me:
private Func<object, object> GetGetDelegate()
{
Type setParamType = typeof(object);
Type[] setParamTypes = { setParamType };
Type setReturnType = typeof(object);
Type owner = _targetType.GetTypeInfo().IsAbstract || _targetType.GetTypeInfo().IsInterface ? null : _targetType;
var getMethod = owner != null
? new DynamicMethod(Guid.NewGuid().ToString(), setReturnType, setParamTypes, owner, true)
: new DynamicMethod(Guid.NewGuid().ToString(), setReturnType, setParamTypes, true);
// From the method, get an ILGenerator. This is used to
// emit the IL that we want.
ILGenerator getIL = getMethod.GetILGenerator();
getIL.Emit(OpCodes.Ldarg_0); //Load the first argument (target object)
if (_targetType.IsValueType)
getIL.Emit(OpCodes.Unbox, _targetType); //unbox struct
else
getIL.Emit(OpCodes.Castclass, this._targetType); //Cast to the source type
Type returnType = null;
if (IsField(_member))
{
getIL.Emit(OpCodes.Ldfld, (FieldInfo)_member);
returnType = _memberType;
}
else
{
var targetGetMethod = ((PropertyInfo)_member).GetGetMethod();
var opCode = _targetType.IsValueType ? OpCodes.Call : OpCodes.Callvirt;
getIL.Emit(opCode, targetGetMethod);
returnType = targetGetMethod.ReturnType;
}
if (returnType.IsValueType)
{
getIL.Emit(OpCodes.Box, returnType);
}
getIL.Emit(OpCodes.Ret);
var del = getMethod.CreateDelegate(Expression.GetFuncType(setParamType, setReturnType));
return del as Func<object, object>;
}
I am trying to create a c# class dynamically at run time.
using System;
class Hist
{
private int? _min;
private int? _max;
public int? min
{
get{return _min;}
set {_min = value;}
}
public int? max
{
get{return _max;}
set {_max = value;}
}
}
public class ProcessData
{
private string _id;
private string _source;
private int? _currentValue;
private Hist _hist;
public Hist hist
{
get { return _hist; }
set{ _hist = value; }
}
public string id
{
get {return _id;}
set { _id = value; }
}
public string source
{
get {return _source;}
set { _source = value; }
}
public int? currentValue
{
get {return _currentValue;}
set { _currentValue = value; }
}
public int? min
{
get { return (hist != null) ? hist.min : null; }
}
public int? max
{
get { return (hist != null) ? hist.max : null; }
}
}
But i am unable to do this specifically.
return (hist != null) ? hist.max : null;
i just need is the get method for any of the min or max property of ProcessData class.
My code for above task:
var method = parentType.GetMethod("get_" + propertyName);
getPropMthdBldr = tb.DefineMethod("get_" + propertyName,
MethodAttributes.Public |
MethodAttributes.SpecialName |
MethodAttributes.HideBySig,
propertyType, Type.EmptyTypes);
getIl = getPropMthdBldr.GetILGenerator();
var moveTo = getIl.DefineLabel();
getIl.Emit(OpCodes.Ldarg_0);
getIl.EmitCall(OpCodes.Call, parentGetMethod, Type.EmptyTypes);
getIl.Emit(OpCodes.Brtrue_S, moveTo);
getIl.Emit(OpCodes.Ldloca_S, 0);
getIl.Emit(OpCodes.Initobj, typeof(int?));
getIl.Emit(OpCodes.Ldloc_0);
getIl.Emit(OpCodes.Ret);
getIl.MarkLabel(moveTo);
getIl.Emit(OpCodes.Ldarg_0);
getIl.EmitCall(OpCodes.Call, parentGetMethod,Type.EmptyTypes);
getIl.EmitCall(OpCodes.Callvirt, method,Type.EmptyTypes);
getIl.Emit(OpCodes.Ret);
Problem is that you are trying to use local variable that not declared:
getIl.Emit(OpCodes.Ldloca_S, 0); // load address of local variable with index 0 on stack
getIl.Emit(OpCodes.Initobj, typeof(int?)); // initialize local variable
getIl.Emit(OpCodes.Ldloc_0); // load value of local variable with index 0 on stack
You can define required local variable like this:
var local = getIl.DeclareLocal(typeof(int?));
And your code will be valid, but to improve readability I would advise you to use variable local instead index. It could be done like this:
getIl.Emit(OpCodes.Ldloca_S, local); // load address of local variable on stack
getIl.Emit(OpCodes.Initobj, typeof(int?)); // initialize local variable
getIl.Emit(OpCodes.Ldloc, local); // load value of local variable on stack
P.S. I'll put here code that I've used for generation of classes Hist and ProcessData, may be it could be useful for you in some way, in case if my explanation was not sufficient.
Main logic in this helper for property creation:
public static class TypeBuilderExtensions
{
public static PropertyBuilder CreateProperty<T>(this TypeBuilder builder, string name) => CreateProperty(builder, typeof(T), name);
public static PropertyBuilder CreateProperty(this TypeBuilder builder, Type propertyType, string name)
{
var field = builder.DefineField($"_{name}", propertyType, FieldAttributes.Private);
var getMethodBuilder = builder.DefineMethod($"get_{name}", MethodAttributes.Public, propertyType, Type.EmptyTypes);
var getGenerator = getMethodBuilder.GetILGenerator();
getGenerator.Emit(OpCodes.Ldarg_0);
getGenerator.Emit(OpCodes.Ldfld, field);
getGenerator.Emit(OpCodes.Ret);
var setMethodBuilder = builder.DefineMethod($"set_{name}", MethodAttributes.Public, typeof(void), new[] { propertyType });
var setGenerator = setMethodBuilder.GetILGenerator();
setGenerator.Emit(OpCodes.Ldarg_0);
setGenerator.Emit(OpCodes.Ldarg_1);
setGenerator.Emit(OpCodes.Stfld, field);
setGenerator.Emit(OpCodes.Ret);
var propertyBuilder = builder.DefineProperty(name, PropertyAttributes.None, propertyType, Type.EmptyTypes);
propertyBuilder.SetGetMethod(getMethodBuilder);
propertyBuilder.SetSetMethod(setMethodBuilder);
return propertyBuilder;
}
public static PropertyBuilder CreateCalculatedProperty<T>(this TypeBuilder builder, string name, MethodInfo getObject, MethodInfo getObjectProperty) => CreateCalculatedProperty(builder, typeof(T), name, getObject, getObjectProperty);
public static PropertyBuilder CreateCalculatedProperty(this TypeBuilder builder, Type propertyType, string name, MethodInfo getObject, MethodInfo getObjectProperty)
{
var getMethodBuilder = builder.DefineMethod($"get_{name}", MethodAttributes.Public, propertyType, Type.EmptyTypes);
var getGenerator = getMethodBuilder.GetILGenerator();
var label = getGenerator.DefineLabel();
var local = getGenerator.DeclareLocal(propertyType);
getGenerator.Emit(OpCodes.Ldarg_0);
getGenerator.Emit(OpCodes.Callvirt, getObject);
getGenerator.Emit(OpCodes.Brtrue, label);
getGenerator.Emit(OpCodes.Ldloca_S, local);
getGenerator.Emit(OpCodes.Initobj, propertyType);
getGenerator.Emit(OpCodes.Ldloc, local);
getGenerator.Emit(OpCodes.Ret);
getGenerator.MarkLabel(label);
getGenerator.Emit(OpCodes.Ldarg_0);
getGenerator.Emit(OpCodes.Callvirt, getObject);
getGenerator.Emit(OpCodes.Callvirt, getObjectProperty);
getGenerator.Emit(OpCodes.Ret);
var propertyBuilder = builder.DefineProperty(name, PropertyAttributes.None, propertyType, Type.EmptyTypes);
propertyBuilder.SetGetMethod(getMethodBuilder);
return propertyBuilder;
}
}
Here is class creation itself:
var assemblyBuilder = AssemblyBuilder.DefineDynamicAssembly(new AssemblyName("TestAssembly"), AssemblyBuilderAccess.Run);
var moduleBuilder = assemblyBuilder.DefineDynamicModule("TestModule");
var histBuilder = moduleBuilder.DefineType("Hist");
var minProperty = histBuilder.CreateProperty<int?>("min");
var maxProperty = histBuilder.CreateProperty<int?>("max");
var processDataBuilder = moduleBuilder.DefineType("ProcessData");
var histProperty = processDataBuilder.CreateProperty(histBuilder, "hist");
processDataBuilder.CreateProperty<string>("id");
processDataBuilder.CreateProperty<string>("source");
processDataBuilder.CreateProperty<int?>("currentValue");
processDataBuilder.CreateCalculatedProperty<int?>("min", histProperty.GetMethod, minProperty.GetMethod);
processDataBuilder.CreateCalculatedProperty<int?>("max", histProperty.GetMethod, maxProperty.GetMethod);
And finally primitive validation of created classes:
void ValidateProperty(object instance, string name, object value, bool setValue = true)
{
var type = instance.GetType();
var property = type.GetProperty(name);
if (setValue) property.SetValue(instance, value);
var result = property.GetValue(instance);
var equals = property.PropertyType.IsValueType && value != null ? value.Equals(result) : value == result;
if (!equals)
throw new InvalidDataException("Property not valid");
}
var histType = histBuilder.CreateType();
var histInstance = Activator.CreateInstance(histType);
ValidateProperty(histInstance, "min", 12);
ValidateProperty(histInstance, "max", 21);
var processDataType = processDataBuilder.CreateType();
var processDataInstance = Activator.CreateInstance(processDataType);
ValidateProperty(processDataInstance, "hist", histInstance);
ValidateProperty(processDataInstance, "id", "Test!");
ValidateProperty(processDataInstance, "source", "Source#");
ValidateProperty(processDataInstance, "currentValue", 126);
ValidateProperty(processDataInstance, "min", 12, false);
ValidateProperty(processDataInstance, "max", 21, false);
ValidateProperty(processDataInstance, "hist", null);
ValidateProperty(processDataInstance, "min", null, false);
ValidateProperty(processDataInstance, "max", null, false);
I'm trying to create this C# class by using System.Reflection.Emit.
private class MyTestData : IMyClonable
{
private int _testValue = 0;
public int testValue
{
get { return _testValue; }
set { _testValue = value; }
}
public IMyClonable Clone()
{
MyTestData clone = new MyTestData();
clone._testValue = _testValue;
return clone ;
}
}
This class must be created from this interface:
public interface IMyTestData : IMyClonable
{
int testValue { get; set; }
}
I already made the code that generate properties and this work's fine. But I stuck when I'm trying to create method Clone(). I don't know how to create an instance of this class itself and save it in a local variable.
Here is the code to generate method Clone():
private static void MakeCloneMethod(Type componentType, TypeBuilder typeBuilder)
{
ConstructorBuilder ctor =
typeBuilder.DefineDefaultConstructor(MethodAttributes.Public);
MethodInfo cloneMethod = typeof(IMyClonable).GetMethod("Clone");
MethodAttributes methodIlAttributes =
(cloneMethod.Attributes & ~MethodAttributes.Abstract) | MethodAttributes.Final;
MethodBuilder cloneMthdBldr = typeBuilder.DefineMethod(
"Clone", methodIlAttributes, typeof(IMyClonable), new Type[] { });
ILGenerator ilgen = cloneMthdBldr.GetILGenerator();
LocalBuilder returnValue = ilgen.DeclareLocal(typeBuilder.AsType());
ilgen.Emit(OpCodes.Newobj, ctor);
ilgen.Emit(OpCodes.Stloc_S, returnValue);
CloneProperties(componentType, ilgen);
ilgen.Emit(OpCodes.Ldloc_S);
ilgen.Emit(OpCodes.Ret);
typeBuilder.DefineMethodOverride(cloneMthdBldr, cloneMethod);
}
private static void CloneProperties(Type componentType, ILGenerator ilgen)
{
PropertyInfo[] allProperties = GetPublicProperties(componentType);
foreach (PropertyInfo propInfo in allProperties)
{
ilgen.Emit(OpCodes.Ldarg_0);
ilgen.Emit(OpCodes.Ldfld, builders[propInfo]);
ilgen.Emit(OpCodes.Stfld, builders[propInfo]);
ilgen.Emit(OpCodes.Ldloc_0);
}
}
I get the System.InvalidProgramException when I try to call Clone() method. Even if I comment call of method CloneProperties(). What I'm doing wrong?
I got it! Here is working code:
private static void MakeCloneMethod(Type componentType, TypeBuilder typeBuilder)
{
Type thisType = typeBuilder.AsType();
Type itype = typeof(IMyClonable);
MethodInfo cloneMthd = itype.GetMethod("Clone");
MethodBuilder cloneMthdBldr = typeBuilder.DefineMethod(
cloneMthd.Name, cloneMthd.Attributes & ~MethodAttributes.Abstract,
itype, new Type[] {});
typeBuilder.DefineMethodOverride(cloneMthdBldr, cloneMthd);
ILGenerator gen = cloneMthdBldr.GetILGenerator();
LocalBuilder returnValue = gen.DeclareLocal(thisType);
gen.Emit(OpCodes.Newobj, typeBuilder.DefineDefaultConstructor(MethodAttributes.Public));
gen.Emit(OpCodes.Stloc_S, returnValue);
PropertyInfo[] allProperties = GetPublicProperties(componentType);
foreach (PropertyInfo propInfo in allProperties)
{
gen.Emit(OpCodes.Ldloc_S, returnValue);
gen.Emit(OpCodes.Ldarg_0);
gen.Emit(OpCodes.Ldfld, builders[propInfo]);
gen.Emit(OpCodes.Stfld, builders[propInfo]);
}
gen.Emit(OpCodes.Ldloc_S, returnValue);
gen.Emit(OpCodes.Ret);
}
Thanks to Svick!
I have this architecture.
public void Init()
{
PropertyInfo[] infos = typeof(Transform).GetProperties(BindingFlags.Public | BindingFlags.Instance);
foreach (PropertyInfo info in infos)
{
// Save getter
MethodInfo method = info.GetGetMethod();
System.Type returnType = method.ReturnType;
System.Func<Transform, Vector3> fact = GetFactory<Transform, Vector3>(method);
Vector3 v = fact(this.Value);
Debug.Log("Test123 " + v);
//_getters.Add(info.Name, newMethod);
}
}
static System.Func<T, T1> GetFactory<T, T1>(MethodInfo info)
{
return (System.Func<T, T1>)GetFactory(typeof(T), typeof(T1), info);
}
static object GetFactory(System.Type type, System.Type type1, MethodInfo info)
{
System.Type funcType = typeof(System.Func<,>).MakeGenericType(type, type1);
return System.Delegate.CreateDelegate(funcType, info);
}
It even works if method.ReturnType is Vector3.
But I want the func<Transform, Vector3> to be func<Transform, ReturnType>.
I have no idea doing this.
Does someone of you know how I can do this?
And I also have no idea how to save the result in a dictionary.
Which type can the dictionary be of?
public Dictionary<string, System.Func<object, object>> _getters = new Dictionary<string, System.Func<object, object>>();
Edit: No ideas?
From what I get from the comment, is that you want to access getters through a string key? If that is the case, you might want to use the code sample below.
The entity you want to access:
class Entity
{
public int Foo { get { return 42; } }
public string Bar { get { return "Magic"; } }
}
The class that allows you to access the properties by name:
class PropertyCaller<T>
{
// Static for each type T
private static readonly IDictionary<string, Func<T, object>> _propertyLookup;
static PropertyCaller()
{
_propertyLookup = new Dictionary<string, Func<T, object>>();
Type objectType = typeof (T);
foreach (PropertyInfo prop in objectType.GetProperties())
{
const BindingFlags num = BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic;
MethodInfo getMethod = prop.GetGetMethod(true);
_propertyLookup.Add(prop.Name, item => getMethod.Invoke(item, num, null, null, null));
}
}
public static object Call(T obj, string propertyName)
{
Func<T, object> f;
if (!_propertyLookup.TryGetValue(propertyName, out f))
{
throw new InvalidOperationException();
}
return f(obj);
}
}
Example usage:
Entity e = new Entity();
var a = PropertyCaller<Entity>.Call(e, "Foo"); // 42
var b = PropertyCaller<Entity>.Call(e, "Bar"); // Magic
Here is the Context :
I try to code a mapper for converting my DomainModel Objects to ViewModel Ojects dynamically. The problem I get, it's when I try to invoke a method of generic class by reflection I get this error :
System.InvalidOperationException : Late bound operations cannot be performed on types or methods for which ContainsGenericParameters is true.
Can someone help-me to figure out where is the fault ? It would be greatly appreciated
Here is the Code (I tried to simplified it) :
public class MapClass<SourceType, DestinationType>
{
public string Test()
{
return test
}
public void MapClassReflection(SourceType source, ref DestinationType destination)
{
Type sourceType = source.GetType();
Type destinationType = destination.GetType();
foreach (PropertyInfo sourceProperty in sourceType.GetProperties())
{
string destinationPropertyName = LookupForPropertyInDestinationType(sourceProperty.Name, destinationType);
if (destinationPropertyName != null)
{
PropertyInfo destinationProperty = destinationType.GetProperty(destinationPropertyName);
if (destinationProperty.PropertyType == sourceProperty.PropertyType)
{
destinationProperty.SetValue(destination, sourceProperty.GetValue(source, null), null);
}
else
{
Type d1 = typeof(MapClass<,>);
Type[] typeArgs = { destinationProperty.GetType(), sourceType.GetType() };
Type constructed = d1.MakeGenericType(typeArgs);
object o = Activator.CreateInstance(constructed, null);
MethodInfo theMethod = d1.GetMethod("Test");
string toto = (string)theMethod.Invoke(o,null);
}
}
}
}
private string LookupForPropertyInDestinationType(string sourcePropertyName, Type destinationType)
{
foreach (PropertyInfo property in destinationType.GetProperties())
{
if (property.Name == sourcePropertyName)
{
return sourcePropertyName;
}
}
return null;
}
}
You need to call GetMethod on the constructed type constructed, not on the type definition d1.
// ...
Type d1 = typeof(MapClass<,>);
Type[] typeArgs = { destinationProperty.GetType(), sourceType.GetType() };
Type constructed = d1.MakeGenericType(typeArgs);
object o = Activator.CreateInstance(constructed, null);
MethodInfo theMethod = constructed.GetMethod("Test");
string toto = (string)theMethod.Invoke(o, null);
// ...