public void If_Else() { DynamicMethod method = new DynamicMethod("temp", typeof(int), Type.EmptyTypes); MethodGenerator g = new MethodGenerator(method); var count = g.Declare <int>(); g.If(() => { g.Load(true); return(LogicOperator.IsTrue); }, () => { g.Assign(count, () => { g.Load(1); }); }, () => { g.Assign(count, () => { g.Load(2); }); }); g.Load(count); g.Return(); int result = (int)method.Invoke(null, null); Assert.AreEqual(1, result); }
public override void GenerateDeserializeIL(MethodGenerator g) { SetMember(g, () => { var count = g.Declare <int>(); g.Assign(count, () => { SerializationMethodHelper.ReadLength(g, this.DTOMemberName);//读取数量 }); var list = g.Declare(this.TargetType); g.If(() => { g.Load(count); g.Load(0); return(LogicOperator.LessThan); }, () => { //数量小于1 //list = new List<T>(); var elementType = this.TargetType.ResolveElementType(); g.Assign(list, () => { g.NewObject(this.TargetType); }); }, () => { //list = new List<T>(); g.Assign(list, () => { g.NewObject(this.TargetType); }); var elementType = this.TargetType.ResolveElementType(); g.For(count, (index) => { var item = g.Declare(elementType); g.Assign(item, () => { SerializationMethodHelper.ReadElement(g, this.DTOMemberName, elementType, index); }); g.Load(list); g.Load(item); g.Call(this.TargetType.ResolveMethod("Add", elementType)); }); }); g.Load(list); }); }
private static PropertyValueMethod GenerateGetPropertyValueMethod <T>(Type objectType, string propertyName) { DynamicMethod method = new DynamicMethod(string.Format("GetPropertyValue_{0}", Guid.NewGuid().ToString("n")) , typeof(T) , new Type[] { typeof(object) } , true); MethodGenerator g = new MethodGenerator(method); var result = g.Declare <T>("result"); g.Assign(result, () => { g.LoadParameter(0); g.Cast(objectType); g.LoadMember(propertyName); }); g.LoadVariable("result"); g.Return(); var invoke = method.CreateDelegate(typeof(Func <object, T>)); return(new PropertyValueMethod(propertyName, invoke)); }
private static PropertyValueMethod GenerateSetPropertyValueMethod(Type objectType, string propertyName) { var propertyInfo = objectType.ResolveProperty(propertyName); var propertyType = propertyInfo.PropertyType; DynamicMethod method = new DynamicMethod(string.Format("SetPropertyValue_{0}", Guid.NewGuid().ToString("n")) , null , new Type[] { typeof(object), typeof(object) } //obj value , true); MethodGenerator g = new MethodGenerator(method); g.LoadParameter(0); g.Cast(objectType); g.Assign(propertyName, () => { g.LoadParameter(1); g.Cast(propertyType); }); g.Return(); var invoke = method.CreateDelegate(typeof(Action <object, object>)); return(new PropertyValueMethod(propertyName, invoke)); }
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 SetMember(MethodGenerator g, Action loadValue) { if (this.IsClassInfo) { g.AssignVariable(SerializationArgs.InstanceName, loadValue); } else { LoadOwner(g); if (this.IsFieldInfo) { g.Assign(this.FieldInfo, loadValue); } else { g.Assign(this.PropertyInfo, loadValue); } } }
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); }); }