private static PropertyValueMethod GenerateGetPropertyValueMethod(Type objectType, string propertyName) { DynamicMethod method = new DynamicMethod(string.Format("GetPropertyValue_{0}", Guid.NewGuid().ToString("n")) , typeof(object) , new Type[] { typeof(object) } , true); MethodGenerator g = new MethodGenerator(method); var result = g.Declare <object>("result"); g.Assign(result, () => { g.LoadParameter(0); g.Cast(objectType); g.LoadMember(propertyName); g.Cast(typeof(object)); }); g.LoadVariable("result"); g.Return(); var invoke = method.CreateDelegate(typeof(Func <object, object>)); return(new PropertyValueMethod(propertyName, invoke)); }
public void If_Condition_And() { DynamicMethod method = new DynamicMethod("temp", typeof(int), Type.EmptyTypes); MethodGenerator g = new MethodGenerator(method); var a = g.Declare(5); g.If(() => {//a<10 && a>6 g.And(() => { g.Load(a); g.Load(10); return(LogicOperator.LessThan); }, () => { g.Load(a); g.Load(6); return(LogicOperator.GreaterThan); }); return(LogicOperator.IsTrue); }, () => { g.Increment(a); }); g.Load(a); g.Return(); int result = (int)method.Invoke(null, new object[] { }); Assert.AreEqual(5, result); }
private static CreateInstanceMethod GenerateCreateInstanceMethod(ConstructorInfo constructor) { var objectType = constructor.DeclaringType; DynamicMethod method = new DynamicMethod(string.Format("CreateInstanceByConstructor_{0}", Guid.NewGuid().ToString("n")) , typeof(object) , new Type[] { typeof(object[]) } , true); MethodGenerator g = new MethodGenerator(method); //以下代码把数组参数转成,new T(arg0,arg1)的形式 var result = g.Declare(objectType, "result"); var objs = g.Declare <object[]>(); g.Assign(objs, () => { g.LoadParameter(0); }); g.Assign(result, () => { g.NewObject(constructor, () => { var index = g.Declare <int>(); var prms = constructor.GetParameters(); for (var i = 0; i < prms.Length; i++) { g.Assign(index, () => { g.Load(i); }); g.LoadElement(objs, index); g.Cast(prms[i].ParameterType); } }); }); g.LoadVariable("result"); g.Cast(typeof(object)); g.Return(); var invoke = method.CreateDelegate(typeof(Func <object[], object>)); return(new CreateInstanceMethod(invoke)); }
public void For() { DynamicMethod method = new DynamicMethod("temp", typeof(int), new Type[] { typeof(List <int>) }); MethodGenerator g = new MethodGenerator(method); IVariable array = g.Declare <List <int> >(); g.Assign(array, () => { g.LoadParameter(0); }); var count = g.Declare <int>(); var number = g.Declare(0); g.Assign(count, () => { g.LoadParameter(0); g.LoadMember("Count"); }); g.For(count, (index) => { g.Assign(number, () => { g.LoadParameter(0); g.Load(index); g.Call(typeof(List <int>).ResolveMethod("get_Item", typeof(int))); g.Load(number); g.Add <int>(); }); }); g.Load(number); g.Return(); List <int> list = new List <int> { 1, 2, 3, 4, 5 }; var result = (int)method.Invoke(null, new object[] { list }); Assert.AreEqual(15, result); }
/// <summary> /// 声明强类型的instance变量留待后续代码使用,避免频繁类型转换 /// </summary> private static void DeclareInstance(MethodGenerator g, TypeSerializationInfo typeInfo) { //TypeClassName instance = (TypeClassName)instance; var instance = g.Declare(typeInfo.ClassType, SerializationArgs.InstanceName); g.Assign(instance, () => { g.LoadParameter(SerializationArgs.InstanceIndex); g.UnboxAny(typeInfo.ClassType); }); }
public void DeclareString() { DynamicMethod method = new DynamicMethod("temp", typeof(string), Type.EmptyTypes); MethodGenerator g = new MethodGenerator(method); IVariable count = g.Declare("哈哈哈哈哈"); g.Load(count); g.Return(); string result = (string)method.Invoke(null, new object[] { }); Assert.AreEqual("哈哈哈哈哈", result); }
public void DeclareInt() { DynamicMethod method = new DynamicMethod("temp", typeof(int), Type.EmptyTypes); MethodGenerator g = new MethodGenerator(method); IVariable count = g.Declare(3); g.Load(count); g.Return(); int result = (int)method.Invoke(null, new object[] {}); Assert.AreEqual(3, result); }
public void While() { DynamicMethod method = new DynamicMethod("temp", typeof(int), Type.EmptyTypes); MethodGenerator g = new MethodGenerator(method); const int maxNumber = 15; var num = g.Declare(0); var max = g.Declare(maxNumber); g.While(() => { g.Load(num); g.Load(max); return(LogicOperator.LessThan); }, () => { g.Increment(num); }); g.Load(num); g.Return(); int result = (int)method.Invoke(null, new object[] { }); Assert.AreEqual(maxNumber, result); }
private static CreateInstanceMethod GenerateCreateInstanceMethod(Type objectType) { DynamicMethod method = new DynamicMethod(string.Format("CreateInstance_{0}", Guid.NewGuid().ToString("n")) , typeof(object) , Array.Empty <Type>() , true); MethodGenerator g = new MethodGenerator(method); var result = g.Declare <object>("result"); g.Assign(result, () => { g.NewObject(objectType); g.Cast(typeof(object)); }); g.LoadVariable("result"); g.Return(); var invoke = method.CreateDelegate(typeof(Func <object>)); return(new CreateInstanceMethod(invoke)); }
public override void GenerateDeserializeIL(MethodGenerator g) { SetMember(g, () => { var array = g.Declare(this.TargetType); if (this.MemberAttribute.IsBlob) { g.Assign(array, () => { SerializationMethodHelper.ReadBlob(g, this.DTOMemberName);//读取数量 }); } else { var length = g.Declare <int>(); g.Assign(length, () => { SerializationMethodHelper.ReadLength(g, this.DTOMemberName);//读取数量 }); g.If(() => { g.Load(length); g.Load(0); return(LogicOperator.LessThan); }, () => { //数量小于1 //array = new array[]; var elementType = this.TargetType.ResolveElementType(); g.Assign(array, () => { g.NewArray(elementType, () => { g.Load(length); }); }); }, () => { var elementType = this.TargetType.ResolveElementType(); //int[] = new int[c]; g.Assign(array, () => { g.NewArray(elementType, () => { g.Load(length); }); }); g.For(length, (index) => { var item = g.Declare(elementType); g.Assign(item, () => { SerializationMethodHelper.ReadElement(g, this.DTOMemberName, elementType, index); }); g.StoreElement(array, index, item); }); }); } g.Load(array); }); }