/// <summary> /// Sets up a property (IEnumerable) /// </summary> /// <param name="Method">Method builder</param> /// <param name="BaseType">Base type for the object</param> /// <param name="ReturnValue">Return value</param> /// <param name="Property">Property info</param> /// <param name="Mapping">Mapping info</param> private void SetupIEnumerableProperty(IMethodBuilder Method, Type BaseType, Reflection.Emit.BaseClasses.VariableBase ReturnValue, IProperty Property, IMapping Mapping) { Utilities.Reflection.Emit.FieldBuilder Field = Fields.Find(x => x.Name == Property.DerivedFieldName); Utilities.Reflection.Emit.Commands.If If1 = Method.If((VariableBase)SessionField, Comparison.NotEqual, null); { Utilities.Reflection.Emit.Commands.If If2 = Method.If(Field, Comparison.Equal, null); { //Load data VariableBase IDValue = Method.This.Call(BaseType.GetProperty(Mapping.IDProperty.Name).GetGetMethod()); VariableBase IDParameter = Method.NewObj(typeof(EqualParameter <>).MakeGenericType(Mapping.IDProperty.Type), new object[] { IDValue, "ID", "@" }); VariableBase PropertyList = Method.NewObj(typeof(List <IParameter>)); PropertyList.Call("Add", new object[] { IDParameter }); MethodInfo LoadPropertiesMethod = typeof(Session).GetMethod("LoadProperties"); LoadPropertiesMethod = LoadPropertiesMethod.MakeGenericMethod(new Type[] { BaseType, Field.DataType.GetGenericArguments()[0] }); VariableBase ReturnVal = ((VariableBase)SessionField).Call(LoadPropertiesMethod, new object[] { Method.This, Property.Name, PropertyList.Call("ToArray") }); Field.Assign(ReturnVal); } If2.EndIf(); Utilities.Reflection.Emit.Commands.If If3 = Method.If(Field, Comparison.Equal, null); { Field.Assign(Method.NewObj(typeof(List <>).MakeGenericType(Property.Type).GetConstructor(Type.EmptyTypes))); } If3.EndIf(); } If1.EndIf(); ReturnValue.Assign(Field); }
public void If() { Utilities.Reflection.Emit.Assembly Assembly = new Utilities.Reflection.Emit.Assembly("TestAssembly"); Utilities.Reflection.Emit.TypeBuilder TestType = Assembly.CreateType("TestType"); IMethodBuilder Method = TestType.CreateMethod("TestMethod"); VariableBase Local1 = Method.CreateLocal("Local1", typeof(int)); VariableBase Local2 = Method.CreateLocal("Local2", typeof(int)); Utilities.Reflection.Emit.Commands.If If = Method.If(Local1, Utilities.Reflection.Emit.Enums.Comparison.Equal, Local2); Method.EndIf(If); Assert.NotNull(If); }
private void SetupEnd(IMethodBuilder Method, VariableBase ReturnValue, IPropertyBuilder AspectusEnding) { VariableBase EndingArgs = Method.NewObj(typeof(Ending).GetConstructor(new Type[0])); EndingArgs.Call(typeof(Ending).GetProperty("MethodName").GetSetMethod(), new object[] { Method.Name }); if (Method.ReturnType != typeof(void) && ReturnValue.DataType != null && ReturnValue.DataType.IsValueType) { EndingArgs.Call(typeof(Ending).GetProperty("ReturnValue").GetSetMethod(), new object[] { Method.Box(ReturnValue) }); } else if (Method.ReturnType != typeof(void)) { EndingArgs.Call(typeof(Ending).GetProperty("ReturnValue").GetSetMethod(), new object[] { ReturnValue }); } VariableBase ParameterList = EndingArgs.Call(typeof(Ending).GetProperty("Parameters").GetGetMethod()); for (int x = 1; x < Method.Parameters.Count; ++x) { if (Method.Parameters[x].DataType != null && Method.Parameters[x].DataType.IsValueType) { ParameterList.Call(typeof(List <object>).GetMethod("Add"), new object[] { Method.Box(Method.Parameters[x]) }); } else { ParameterList.Call(typeof(List <object>).GetMethod("Add"), new object[] { Method.Parameters[x] }); } } VariableBase IEventsThis = Method.Cast(Method.This, typeof(IEvents)); Type EventHelperType = typeof(Utilities.Events.EventHelper); MethodInfo[] Methods = EventHelperType.GetMethods() .Where <MethodInfo>(x => x.GetParameters().Length == 3) .ToArray(); MethodInfo TempMethod = Methods.Length > 0 ? Methods[0] : null; TempMethod = TempMethod.MakeGenericMethod(new Type[] { typeof(Ending) }); Method.Call(null, TempMethod, new object[] { AspectusEnding, IEventsThis, EndingArgs }); if (Method.ReturnType != typeof(void)) { VariableBase TempReturnValue = EndingArgs.Call(typeof(Ending).GetProperty("ReturnValue").GetGetMethod()); VariableBase TempNull = Method.CreateLocal("TempNull", typeof(object)); Utilities.Reflection.Emit.Commands.If If = Method.If(TempReturnValue, Utilities.Reflection.Emit.Enums.Comparison.NotEqual, TempNull); { ReturnValue.Assign(TempReturnValue); } Method.SetCurrentMethod(); If.EndIf(); } }
private static void SetupStart(IMethodBuilder Method, System.Reflection.Emit.Label EndLabel, VariableBase ReturnValue, IPropertyBuilder AspectusStarting) { VariableBase StartingArgs = Method.NewObj(typeof(Starting).GetConstructor(new Type[0])); StartingArgs.Call(typeof(Starting).GetProperty("MethodName").GetSetMethod(), new object[] { Method.Name }); VariableBase ParameterList = StartingArgs.Call(typeof(Starting).GetProperty("Parameters").GetGetMethod()); for (int x = 1; x < Method.Parameters.Count; ++x) { if (Method.Parameters.ElementAt(x).DataType != null && Method.Parameters.ElementAt(x).DataType.IsValueType) { ParameterList.Call(typeof(List <object>).GetMethod("Add"), new object[] { Method.Box(Method.Parameters.ElementAt(x)) }); } else { ParameterList.Call(typeof(List <object>).GetMethod("Add"), new object[] { Method.Parameters.ElementAt(x) }); } } VariableBase IEventsThis = Method.Cast(Method.This, typeof(IEvents)); Type EventHelperType = typeof(Utilities.DataTypes.ExtensionMethods.DelegateExtensions); MethodInfo[] Methods = EventHelperType.GetMethods() .Where <MethodInfo>(x => x.GetParameters().Length == 3) .ToArray(); MethodInfo TempMethod = Methods.Length > 0 ? Methods[0] : null; TempMethod = TempMethod.MakeGenericMethod(new Type[] { typeof(Starting) }); Method.Call(null, TempMethod, new object[] { AspectusStarting, IEventsThis, StartingArgs }); if (Method.ReturnType != typeof(void)) { VariableBase TempReturnValue = StartingArgs.Call(typeof(Starting).GetProperty("ReturnValue").GetGetMethod()); VariableBase TempNull = Method.CreateLocal("TempNull", typeof(object)); Utilities.Reflection.Emit.Commands.If If = Method.If(TempReturnValue, Utilities.Reflection.Emit.Enums.Comparison.NotEqual, TempNull); { ReturnValue.Assign(TempReturnValue); Method.Generator.Emit(System.Reflection.Emit.OpCodes.Br, EndLabel); } Method.SetCurrentMethod(); If.EndIf(); } }