Example #1
0
 /// <summary>
 /// Constructor
 /// </summary>
 /// <param name="ObjectCallingOn">Object calling on</param>
 /// <param name="Method">Method builder</param>
 /// <param name="MethodCalling">Method calling on the object</param>
 /// <param name="Parameters">List of parameters to send in</param>
 public Call(IMethodBuilder Method, VariableBase ObjectCallingOn, MethodInfo MethodCalling, object[] Parameters)
     : base()
 {
     this.ObjectCallingOn = ObjectCallingOn;
     this.MethodCalling = MethodCalling;
     this.MethodCallingFrom = Method;
     if (MethodCalling.ReturnType != null && MethodCalling.ReturnType != typeof(void))
     {
         Result = Method.CreateLocal(MethodCalling.Name + "ReturnObject"+YunShop.Utilities.Reflection.Emit.BaseClasses.MethodBase.ObjectCounter.ToString(), MethodCalling.ReturnType);
     }
     if (Parameters != null)
     {
         this.Parameters = new VariableBase[Parameters.Length];
         for (int x = 0; x < Parameters.Length; ++x)
         {
             if (Parameters[x] is VariableBase)
                 this.Parameters[x] = (VariableBase)Parameters[x];
             else
                 this.Parameters[x] = MethodCallingFrom.CreateConstant(Parameters[x]);
         }
     }
     else
     {
         this.Parameters = null;
     }
 }
Example #2
0
 /// <summary>
 ///     Constructor
 /// </summary>
 /// <param name="objectCallingOn">Object calling on</param>
 /// <param name="method">Method builder</param>
 /// <param name="methodCalling">Method calling on the object</param>
 /// <param name="parameters">List of parameters to send in</param>
 public Call(IMethodBuilder method, VariableBase objectCallingOn, MethodInfo methodCalling, object[] parameters)
 {
     ObjectCallingOn = objectCallingOn;
     MethodCalling = methodCalling;
     MethodCallingFrom = method;
     if (methodCalling.ReturnType != null && methodCalling.ReturnType != typeof (void))
     {
         Result =
             method.CreateLocal(
                 methodCalling.Name + "ReturnObject" +
                 MethodBase.ObjectCounter.ToString(CultureInfo.InvariantCulture), methodCalling.ReturnType);
     }
     if (parameters != null)
     {
         Parameters = new VariableBase[parameters.Length];
         for (int x = 0; x < parameters.Length; ++x)
         {
             if (parameters[x] is VariableBase)
                 Parameters[x] = (VariableBase) parameters[x];
             else
                 Parameters[x] = MethodCallingFrom.CreateConstant(parameters[x]);
         }
     }
     else
     {
         Parameters = null;
     }
 }
        public void Add()
        {
            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));

            Assert.NotNull(Method.Add(Local1, Local2));
        }
        public void NewObj()
        {
            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));

            Assert.NotNull(Method.NewObj(typeof(DateTime), new object[] { Local1 }));
        }
Example #5
0
        public void Cast()
        {
            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));

            Method.Cast(Method.This, typeof(object));
        }
Example #6
0
        public void Throw()
        {
            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));

            Method.Throw(Method.NewObj(typeof(Exception)));
        }
Example #7
0
        public void Return()
        {
            Utilities.Reflection.Emit.Assembly    Assembly = new Utilities.Reflection.Emit.Assembly("TestAssembly");
            Utilities.Reflection.Emit.TypeBuilder TestType = Assembly.CreateType("TestType");
            IMethodBuilder Method = TestType.CreateMethod("TestMethod", ReturnType: typeof(int));
            VariableBase   Local1 = Method.CreateLocal("Local1", typeof(int));
            VariableBase   Local2 = Method.CreateLocal("Local2", typeof(int));

            Method.Return(Local1);
        }
        public void Save()
        {
            Utilities.Reflection.Emit.Assembly    Assembly = new Utilities.Reflection.Emit.Assembly("TestAssembly");
            Utilities.Reflection.Emit.TypeBuilder TestType = Assembly.CreateType("TestType");
            IMethodBuilder Method = TestType.CreateConstructor();

            Utilities.Reflection.Emit.PropertyBuilder TestProperty = (Utilities.Reflection.Emit.PropertyBuilder)TestType.CreateProperty("TestProperty", typeof(int));
            Assert.Throws <NullReferenceException>(() => TestProperty.Save(null));
            Assert.DoesNotThrow <Exception>(() => TestProperty.Save(Method.Generator));
        }
Example #9
0
 public virtual void InjectOptimization(IExpression expr)
 {
     _editor.AddInterface(TypeRef(typeof (IDb4oEnhancedFilter)));
     _builder = _editor.NewPublicMethod(PlatformName(OptimizeQueryMethodName), TypeRef
         (typeof (void)), new[] {TypeRef(typeof (IQuery))});
     var predicateClass = _editor.Type;
     expr.Accept(new SODAExpressionBuilder(this, predicateClass));
     _builder.Pop();
     _builder.EndMethod();
 }
        public void Assign()
        {
            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));

            Assert.DoesNotThrow <Exception>(() => Method.Assign(Local1, Local2));
        }
 /// <summary>
 /// Constructor
 /// </summary>
 /// <param name="MethodBuilder">Method builder</param>
 /// <param name="Name">Name of the local</param>
 /// <param name="LocalType">Type of the local</param>
 public LocalBuilder(IMethodBuilder MethodBuilder, string Name, Type LocalType)
     : base()
 {
     Contract.Requires<ArgumentNullException>(MethodBuilder!=null,"MethodBuilder");
     Contract.Requires<ArgumentNullException>(!string.IsNullOrEmpty(Name),"Name");
     this.Name = Name;
     this.MethodBuilder = MethodBuilder;
     this.DataType = LocalType;
     Builder = MethodBuilder.Generator.DeclareLocal(LocalType);
 }
Example #12
0
 /// <summary>
 ///     Constructor
 /// </summary>
 /// <param name="methodBuilder">Method builder</param>
 /// <param name="name">Name of the local</param>
 /// <param name="localType">Type of the local</param>
 public LocalBuilder(IMethodBuilder methodBuilder, string name, Type localType)
 {
     if (methodBuilder == null)
         throw new ArgumentNullException("methodBuilder");
     if (string.IsNullOrEmpty(name))
         throw new ArgumentNullException("name");
     Name = name;
     MethodBuilder = methodBuilder;
     DataType = localType;
     Builder = methodBuilder.Generator.DeclareLocal(localType);
 }
        public void Load()
        {
            Utilities.Reflection.Emit.Assembly    Assembly = new Utilities.Reflection.Emit.Assembly("TestAssembly");
            Utilities.Reflection.Emit.TypeBuilder TestType = Assembly.CreateType("TestType");
            IMethodBuilder Method = TestType.CreateConstructor();

            Assert.NotNull(Method.This);
            Assert.Null(Method.This.DataType);
            Assert.Equal("this", Method.This.Name);
            Assert.DoesNotThrow <Exception>(() => Method.This.Load(Method.Generator));
        }
Example #14
0
        public void Create()
        {
            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));

            Assert.NotNull(Local1);
            Assert.Equal(typeof(int), Local1.DataType);
            Assert.Equal("Local1", Local1.Name);
        }
Example #15
0
        public void Save()
        {
            Utilities.Reflection.Emit.Assembly    Assembly = new Utilities.Reflection.Emit.Assembly("TestAssembly");
            Utilities.Reflection.Emit.TypeBuilder TestType = Assembly.CreateType("TestType");
            IMethodBuilder Method = TestType.CreateConstructor();

            Assert.NotNull(Method.This);
            Assert.Null(Method.This.DataType);
            Assert.Equal("this", Method.This.Name);
            Method.This.Save(Method.Generator);
        }
Example #16
0
        public void Call()
        {
            Utilities.Reflection.Emit.Assembly    Assembly = new Utilities.Reflection.Emit.Assembly("TestAssembly");
            Utilities.Reflection.Emit.TypeBuilder TestType = Assembly.CreateType("TestType");
            IMethodBuilder Method  = TestType.CreateMethod("TestMethod");
            IMethodBuilder Method2 = TestType.CreateMethod("TestMethod2");
            VariableBase   Local1  = Method.CreateLocal("Local1", typeof(int));
            VariableBase   Local2  = Method.CreateLocal("Local2", typeof(int));

            Assert.DoesNotThrow(() => Method.This.Call(Method2, null));
        }
Example #17
0
        public virtual void InjectOptimization(IExpression expr)
        {
            _editor.AddInterface(TypeRef(typeof(IDb4oEnhancedFilter)));
            _builder = _editor.NewPublicMethod(PlatformName(OptimizeQueryMethodName), TypeRef
                                                   (typeof(void)), new ITypeRef[] { TypeRef(typeof(IQuery)) });
            ITypeRef predicateClass = _editor.Type;

            expr.Accept(new SODAMethodBuilder.SODAExpressionBuilder(this, predicateClass));
            _builder.Pop();
            _builder.EndMethod();
        }
Example #18
0
        private void WrapMethodBody(MethodSignature signature, IMethodBuilder bodyWriter)
        {
            WriteLine(signature.ToString(null, null));
            WriteLine("{");
            PushIndent("    ");

            Write(bodyWriter.GetMethodBody());

            PopIndent();
            WriteLine("}");
        }
 /// <summary>
 /// Constructor
 /// </summary>
 /// <param name="MethodBuilder">Method builder</param>
 /// <param name="Name">Name of the local</param>
 /// <param name="LocalType">Type of the local</param>
 public LocalBuilder(IMethodBuilder MethodBuilder, string Name, Type LocalType)
     : base()
 {
     if (MethodBuilder == null)
         throw new ArgumentNullException("MethodBuilder");
     if (string.IsNullOrEmpty(Name))
         throw new ArgumentNullException("Name");
     this.Name = Name;
     this.MethodBuilder = MethodBuilder;
     this.DataType = LocalType;
     Builder = MethodBuilder.Generator.DeclareLocal(LocalType);
 }
 public CreateInterfaceType(IMethodFilter methodFilter,
                            IMethodBuilder methodBuilder,
                            IDependencyScope scope,
                            TypeDefinition targetDependency,
                            ModuleDefinition module)
 {
     _methodFilter     = methodFilter;
     _methodBuilder    = methodBuilder;
     _scope            = scope;
     _targetDependency = targetDependency;
     _module           = module;
 }
Example #21
0
        private static 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.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 eventsThis      = method.Cast(method.This, typeof(IEvents));
            Type         eventHelperType = typeof(DelegateExtensions);

            MethodInfo[] methods = eventHelperType.GetMethods()
                                   .Where(x => x.GetParameters().Length == 3)
                                   .ToArray();
            MethodInfo tempMethod = methods.Length > 0 ? methods[0] : null;

            tempMethod = tempMethod.MakeGenericMethod(new[] { typeof(Ending) });
            method.Call(null, tempMethod, new object[] { aspectusEnding, eventsThis, endingArgs });
            if (method.ReturnType != typeof(void))
            {
                VariableBase tempReturnValue = endingArgs.Call(typeof(Ending).GetProperty("ReturnValue").GetGetMethod());
                VariableBase tempNull        = method.CreateLocal("TempNull", typeof(object));
                If           If = method.If(tempReturnValue, Comparison.NotEqual, tempNull);
                {
                    returnValue.Assign(tempReturnValue);
                }
                method.SetCurrentMethod();
                If.EndIf();
            }
        }
Example #22
0
 public SynchronisationWrapper(
     ITypeBuilder typeBuilder,
     IMethodBuilder methodBuilder,
     ISynchronisationTypeTranslator synchronisationTypeTranslator,
     TypeDefinition type)
 {
     this.m_TypeBuilder = typeBuilder;
     this.m_MethodBuilder = methodBuilder;
     this.m_SynchronisationTypeTranslator = synchronisationTypeTranslator;
     this.m_Type = type;
     this.m_TraceSource = new TraceSource("SynchronisationWrapper");
 }
Example #23
0
 public SynchronisationWrapper(
     ITypeBuilder typeBuilder,
     IMethodBuilder methodBuilder,
     ISynchronisationTypeTranslator synchronisationTypeTranslator,
     TypeDefinition type)
 {
     this.m_TypeBuilder   = typeBuilder;
     this.m_MethodBuilder = methodBuilder;
     this.m_SynchronisationTypeTranslator = synchronisationTypeTranslator;
     this.m_Type        = type;
     this.m_TraceSource = new TraceSource("SynchronisationWrapper");
 }
Example #24
0
        public void While()
        {
            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.While While = Method.While(Local1, Utilities.Reflection.Emit.Enums.Comparison.Equal, Local2);
            Method.EndWhile(While);
            Assert.NotNull(While);
        }
 public CreateInterfaceType(IMethodFilter methodFilter, 
                            IMethodBuilder methodBuilder, 
                            IDependencyScope scope, 
                            TypeDefinition targetDependency,
                            ModuleDefinition module)
 {
     _methodFilter = methodFilter;
     _methodBuilder = methodBuilder;
     _scope = scope;
     _targetDependency = targetDependency;
     _module = module;
 }
Example #26
0
 /// <summary>
 /// Sets up a property (List)
 /// </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 SetupListProperty(IMethodBuilder Method, Type BaseType, VariableBase ReturnValue, IProperty Property, IMapping Mapping)
 {
     Utilities.Reflection.Emit.FieldBuilder Field       = Fields.Find(x => x.Name == Property.DerivedFieldName);
     Utilities.Reflection.Emit.FieldBuilder FieldLoaded = Fields.Find(x => x.Name == Property.DerivedFieldName + "_Loaded");
     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);
         {
             Utilities.Reflection.Emit.Commands.If If3 = Method.If(FieldLoaded, Comparison.Equal, Method.CreateConstant(false));
             {
                 //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("LoadListProperties");
                 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);
                 FieldLoaded.Assign(true);
             }
             If3.EndIf();
         }
         If2.EndIf();
         PropertyInfo CountProperty = Field.DataType.GetProperty("Count");
         Utilities.Reflection.Emit.Commands.If If4 = Method.If(Field.Call(CountProperty.GetGetMethod()), Comparison.Equal, Method.CreateConstant(0));
         {
             Utilities.Reflection.Emit.Commands.If If3 = Method.If(FieldLoaded, Comparison.Equal, Method.CreateConstant(false));
             {
                 //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);
                 FieldLoaded.Assign(true);
             }
             If3.EndIf();
         }
         If4.EndIf();
         Utilities.Reflection.Emit.Commands.If If5 = Method.If(Field, Comparison.Equal, null);
         {
             Field.Assign(Method.NewObj(typeof(List <>).MakeGenericType(Property.Type).GetConstructor(Type.EmptyTypes)));
         }
         If5.EndIf();
     }
     If1.EndIf();
     ReturnValue.Assign(Field);
 }
Example #27
0
        /// <inheritdoc />
        public IMethodBuilder Getter()
        {
            if (this.GetMethod == null)
            {
                this.GetMethod = this.typeBuilder
                                 .NewMethod($"get_{this.name}")
                                 .CallingConvention(this.callingConvention)
                                 .SpecialName()
                                 .Returns(this.propertyType);
            }

            return(this.GetMethod);
        }
Example #28
0
        public void TryCatch()
        {
            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.Try   Try   = Method.Try();
            Utilities.Reflection.Emit.Commands.Catch Catch = Method.Catch(typeof(Exception));
            Method.EndTry();
            Assert.NotNull(Try);
            Assert.NotNull(Catch);
        }
Example #29
0
        public void CreateDefaultConstructor()
        {
            Utilities.Reflection.Emit.Assembly    Assembly = new Utilities.Reflection.Emit.Assembly("TestAssembly");
            Utilities.Reflection.Emit.TypeBuilder TestType = Assembly.CreateType("TestType");
            IMethodBuilder TestConstructor = TestType.CreateDefaultConstructor();

            Assert.NotNull(TestConstructor);
            Assert.Equal(MethodAttributes.Public, TestConstructor.Attributes);
            Assert.Null(TestConstructor.Generator);
            Assert.Null(TestConstructor.Name);
            Assert.Equal(1, TestConstructor.Parameters.Count);
            Assert.Null(TestConstructor.ReturnType);
            Assert.NotNull(TestConstructor.This);
        }
        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();
            }
        }
Example #31
0
        private static void SetupMethod(Type baseType, IMethodBuilder method, IPropertyBuilder aspectusStarting,
                                        IPropertyBuilder aspectusEnding, IPropertyBuilder aspectusException,
                                        MethodInfo baseMethod)
        {
            if (baseMethod == null)
            {
                baseMethod = baseType.GetMethod(method.Name);
            }
            method.SetCurrentMethod();
            Label        endLabel    = method.Generator.DefineLabel();
            VariableBase returnValue = method.ReturnType != typeof(void)
                                           ? method.CreateLocal("FinalReturnValue", method.ReturnType)
                                           : null;
            Try Try = method.Try();
            {
                SetupStart(method, endLabel, returnValue, aspectusStarting);
                _aspects.ForEach(x => x.SetupStartMethod(method, baseType));
                var parameters = new List <ParameterBuilder>();
                method.Parameters.For(1, method.Parameters.Count - 1, x => parameters.Add(x));
                if (method.ReturnType != typeof(void) && baseMethod != null)
                {
                    returnValue.Assign(method.This.Call(baseMethod, parameters.ToArray()));
                }
                else if (baseMethod != null)
                {
                    method.This.Call(baseMethod, parameters.ToArray());
                }
                SetupEnd(method, returnValue, aspectusEnding);
                _aspects.ForEach(x => x.SetupEndMethod(method, baseType, returnValue));
                method.Generator.MarkLabel(endLabel);
            }
            Catch Catch = Try.StartCatchBlock(typeof(System.Exception));

            {
                SetupException(method, Catch, aspectusException);
                _aspects.ForEach(x => x.SetupExceptionMethod(method, baseType));
                Catch.Rethrow();
            }
            Try.EndTryBlock();

            if (method.ReturnType != typeof(void))
            {
                method.Return(returnValue);
            }
            else
            {
                method.Return();
            }
        }
Example #32
0
 /// <summary>
 ///     Constructor
 /// </summary>
 /// <param name="methodBuilder">Method builder</param>
 /// <param name="name">Name of the local</param>
 /// <param name="localType">Type of the local</param>
 public LocalBuilder(IMethodBuilder methodBuilder, string name, Type localType)
 {
     if (methodBuilder == null)
     {
         throw new ArgumentNullException("methodBuilder");
     }
     if (string.IsNullOrEmpty(name))
     {
         throw new ArgumentNullException("name");
     }
     Name          = name;
     MethodBuilder = methodBuilder;
     DataType      = localType;
     Builder       = methodBuilder.Generator.DeclareLocal(localType);
 }
Example #33
0
 /// <summary>
 /// Constructor
 /// </summary>
 /// <param name="MethodBuilder">Method builder</param>
 /// <param name="Name">Name of the local</param>
 /// <param name="LocalType">Type of the local</param>
 public LocalBuilder(IMethodBuilder MethodBuilder, string Name, Type LocalType)
     : base()
 {
     if (MethodBuilder == null)
     {
         throw new ArgumentNullException("MethodBuilder");
     }
     if (string.IsNullOrEmpty(Name))
     {
         throw new ArgumentNullException("Name");
     }
     this.Name          = Name;
     this.MethodBuilder = MethodBuilder;
     this.DataType      = LocalType;
     Builder            = MethodBuilder.Generator.DeclareLocal(LocalType);
 }
Example #34
0
        private void DefineGenericParameters(CodeGenerationContext context, IMethodBuilder methodBuilder, MutableMethodInfo method)
        {
            if (!method.IsGenericMethodDefinition)
            {
                return;
            }

            var genericParameterNames     = method.MutableGenericParameters.Select(p => p.Name).ToArray();
            var genericParametersBuilders = methodBuilder.DefineGenericParameters(genericParameterNames);

            foreach (var pair in genericParametersBuilders.Zip(method.MutableGenericParameters, (b, g) => new { Builder = b, GenericParameter = g }))
            {
                pair.Builder.RegisterWith(context.EmittableOperandProvider, pair.GenericParameter);
                DefineGenericParameter(pair.Builder, pair.GenericParameter);
            }
        }
        private void SetupException(IMethodBuilder Method, Utilities.Reflection.Emit.Commands.Catch Catch, IPropertyBuilder AspectusException)
        {
            VariableBase ExceptionArgs = Method.NewObj(typeof(Utilities.Reflection.AOP.EventArgs.Exception).GetConstructor(new Type[0]));

            ExceptionArgs.Call(typeof(Utilities.Reflection.AOP.EventArgs.Exception).GetProperty("InternalException").GetSetMethod(), new object[] { Catch.Exception });
            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(Utilities.Reflection.AOP.EventArgs.Exception) });
            Method.Call(null, TempMethod, new object[] { AspectusException, IEventsThis, ExceptionArgs });
        }
Example #36
0
        private static void SetupException(IMethodBuilder method, Catch Catch, IPropertyBuilder aspectusException)
        {
            VariableBase exceptionArgs = method.NewObj(typeof(Exception).GetConstructor(new Type[0]));

            exceptionArgs.Call(typeof(Exception).GetProperty("InternalException").GetSetMethod(),
                               new object[] { Catch.Exception });
            VariableBase eventsThis      = method.Cast(method.This, typeof(IEvents));
            Type         eventHelperType = typeof(DelegateExtensions);

            MethodInfo[] methods = eventHelperType.GetMethods()
                                   .Where(x => x.GetParameters().Length == 3)
                                   .ToArray();
            MethodInfo tempMethod = methods.Length > 0 ? methods[0] : null;

            tempMethod = tempMethod.MakeGenericMethod(new[] { typeof(Exception) });
            method.Call(null, tempMethod, new object[] { aspectusException, eventsThis, exceptionArgs });
        }
        /// <summary>
        /// Resolves any Mvc attributes on the method.
        /// </summary>
        /// <param name="methodBuilder">The method being built.</param>
        /// <param name="methodInfo">The method being called.</param>
        /// <returns>True if any resolved; otherwise false.</returns>
        public static bool ResolveMvcAttributes(
            this IMethodBuilder methodBuilder,
            MethodInfo methodInfo)
        {
            HttpGetAttribute getAttr = methodInfo.GetCustomAttribute <HttpGetAttribute>(true);

            if (getAttr != null)
            {
                methodBuilder.SetCustomAttribute(AttributeUtility.BuildAttribute <string, HttpGetAttribute>(getAttr.Template));
                return(true);
            }

            HttpPostAttribute postAttr = methodInfo.GetCustomAttribute <HttpPostAttribute>(true);

            if (postAttr != null)
            {
                methodBuilder.SetCustomAttribute(AttributeUtility.BuildAttribute <string, HttpPostAttribute>(postAttr.Template));
                return(true);
            }

            HttpPutAttribute putAttr = methodInfo.GetCustomAttribute <HttpPutAttribute>(true);

            if (putAttr != null)
            {
                methodBuilder.SetCustomAttribute(AttributeUtility.BuildAttribute <string, HttpPutAttribute>(putAttr.Template));
                return(true);
            }

            HttpPatchAttribute patchAttr = methodInfo.GetCustomAttribute <HttpPatchAttribute>(true);

            if (patchAttr != null)
            {
                methodBuilder.SetCustomAttribute(AttributeUtility.BuildAttribute <string, HttpPatchAttribute>(patchAttr.Template));
                return(true);
            }

            HttpDeleteAttribute deleteAttr = methodInfo.GetCustomAttribute <HttpDeleteAttribute>(true);

            if (deleteAttr != null)
            {
                methodBuilder.SetCustomAttribute(AttributeUtility.BuildAttribute <string, HttpDeleteAttribute>(deleteAttr.Template));
                return(true);
            }

            return(false);
        }
Example #38
0
        /// <summary>
        /// Processes method attributes.
        /// </summary>
        /// <param name="methodBuilder">A method builder.</param>
        /// <param name="methodInfo">The method.</param>
        /// <returns>The method builder.</returns>
        public static IMethodBuilder ProcessAttributes(
            this IMethodBuilder methodBuilder,
            MethodInfo methodInfo)
        {
            foreach (var attr in methodInfo.GetCustomAttributes())
            {
                if (attr is ProducesAttribute)
                {
                    methodBuilder.SetCustomAttribute(
                        AttributeUtility.BuildAttribute <Type, ProducesAttribute>(
                            ((ProducesAttribute)attr).Type,
                            () => AttributeUtility.GetAttributePropertyValues <ProducesAttribute>((ProducesAttribute)attr, new[] { "Type", "ContentTypes" })));
                }
                else if (attr is ProducesResponseTypeAttribute)
                {
                    methodBuilder.SetCustomAttribute(
                        AttributeUtility.BuildAttribute <int, ProducesResponseTypeAttribute>(
                            ((ProducesResponseTypeAttribute)attr).StatusCode,
                            () => AttributeUtility.GetAttributePropertyValues <ProducesResponseTypeAttribute>((ProducesResponseTypeAttribute)attr, new[] { "type" })));
                }

/*
 *              else if (attr is SwaggerParameterAttribute)
 *              {
 *                  methodBuilder.SetCustomAttribute(
 *                      AttributeUtility.BuildAttribute<string, SwaggerParameterAttribute>(
 *                          ((SwaggerParameterAttribute)attr).ParameterName,
 *                          () => AttributeUtility.GetAttributePropertyValues<SwaggerParameterAttribute>((SwaggerParameterAttribute)attr, null)));
 *              }
 *              else if (attr is SwaggerRequestHeaderParameterAttribute)
 *              {
 *                  methodBuilder.SetCustomAttribute(
 *                      AttributeUtility.BuildAttribute<string, SwaggerRequestHeaderParameterAttribute>(
 *                          ((SwaggerRequestHeaderParameterAttribute)attr).Header,
 *                          () => AttributeUtility.GetAttributePropertyValues<SwaggerRequestHeaderParameterAttribute>((SwaggerRequestHeaderParameterAttribute)attr, null)));
 *              }
 */
                else if (attr is ObsoleteAttribute)
                {
                    methodBuilder.SetCustomAttribute(
                        AttributeUtility.BuildAttribute <ObsoleteAttribute>(null));
                }
            }

            return(methodBuilder);
        }
Example #39
0
 /// <summary>
 /// Constructor
 /// </summary>
 /// <param name="ObjectCallingOn">Object calling on</param>
 /// <param name="Method">Method builder</param>
 /// <param name="MethodCalling">Method calling on the object</param>
 /// <param name="Parameters">List of parameters to send in</param>
 public Call(IMethodBuilder Method, VariableBase ObjectCallingOn, ConstructorInfo MethodCalling, object[] Parameters)
     : base()
 {
     this.ObjectCallingOn = ObjectCallingOn;
     this.ConstructorCalling = MethodCalling;
     this.MethodCallingFrom = Method;
     if (Parameters != null)
     {
         this.Parameters = new VariableBase[Parameters.Length];
         for (int x = 0; x < Parameters.Length; ++x)
         {
             if (Parameters[x] is VariableBase)
                 this.Parameters[x] = (VariableBase)Parameters[x];
             else
                 this.Parameters[x] = MethodCallingFrom.CreateConstant(Parameters[x]);
         }
     }
     else
     {
         this.Parameters = null;
     }
 }
Example #40
0
 /// <summary>
 ///     Constructor
 /// </summary>
 /// <param name="objectCallingOn">Object calling on</param>
 /// <param name="method">Method builder</param>
 /// <param name="methodCalling">Method calling on the object</param>
 /// <param name="parameters">List of parameters to send in</param>
 public Call(IMethodBuilder method, VariableBase objectCallingOn, ConstructorInfo methodCalling,
             object[] parameters)
 {
     ObjectCallingOn = objectCallingOn;
     ConstructorCalling = methodCalling;
     MethodCallingFrom = method;
     if (parameters != null)
     {
         Parameters = new VariableBase[parameters.Length];
         for (int x = 0; x < parameters.Length; ++x)
         {
             if (parameters[x] is VariableBase)
                 Parameters[x] = (VariableBase) parameters[x];
             else
                 Parameters[x] = MethodCallingFrom.CreateConstant(parameters[x]);
         }
     }
     else
     {
         Parameters = null;
     }
 }
Example #41
0
 /// <summary>
 /// Sets up a property (non 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 SetupSingleProperty(IMethodBuilder Method, Type BaseType, Reflection.Emit.BaseClasses.VariableBase ReturnValue, IProperty Property, IMapping Mapping)
 {
     Company.Utilities.Reflection.Emit.FieldBuilder Field = Fields.Find(x => x.Name == Property.DerivedFieldName);
     Company.Utilities.Reflection.Emit.Commands.If If1 = Method.If((VariableBase)SessionField, Comparison.NotEqual, null);
     {
         Company.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 LoadPropertyMethod = typeof(Session).GetMethod("LoadProperty");
             LoadPropertyMethod = LoadPropertyMethod.MakeGenericMethod(new Type[] { BaseType, Field.DataType });
             VariableBase ReturnVal = ((VariableBase)SessionField).Call(LoadPropertyMethod, new object[] { Method.This, Property.Name, PropertyList.Call("ToArray") });
             Field.Assign(ReturnVal);
         }
         If2.EndIf();
     }
     If1.EndIf();
     ReturnValue.Assign(Field);
 }
Example #42
0
 /// <summary>
 /// Sets up a property (List)
 /// </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 SetupListProperty(IMethodBuilder Method, Type BaseType, VariableBase ReturnValue, IProperty Property, IMapping Mapping)
 {
     Company.Utilities.Reflection.Emit.FieldBuilder Field = Fields.Find(x => x.Name == Property.DerivedFieldName);
     Company.Utilities.Reflection.Emit.Commands.If If1 = Method.If((VariableBase)SessionField, Comparison.NotEqual, null);
     {
         Company.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("LoadListProperties");
             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();
         PropertyInfo CountProperty=Field.DataType.GetProperty("Count");
         Company.Utilities.Reflection.Emit.Commands.If If4 = Method.If(Field.Call(CountProperty.GetGetMethod()), Comparison.Equal, Method.CreateConstant(0));
         {
             //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);
         }
         If4.EndIf();
         Company.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 BuildMethod(IMethodBuilder methodBuilder, Type injectionType)
		{
			if (methodBuilder == null) throw new ArgumentNullException("methodBuilder");
			methodBuilder.Build(_typeBuilder, _injectionSetField, injectionType);
		}
Example #44
0
 public void Build(IMethodBuilder builder)
 {
     //Dump();
     
     ILGenerator il = builder.GetILGenerator();
     
     Dictionary<BlockLabel, Label> labels
         = new Dictionary<BlockLabel, Label>();
     
     Dictionary<BlockLocal, LocalBuilder> locals
         = new Dictionary<BlockLocal, LocalBuilder>();
     
     foreach (IBlockLine line in (IEnumerable<IBlockLine>) lines)
     {
         BlockInstruction instruction = line as BlockInstruction;
         
         if ((instruction != null) && (instruction.Operation == BlockOperation.MarkLabel))
             labels[(BlockLabel) instruction.Parameter] = il.DefineLabel();
     }
     
     foreach (IBlockLine line in (IEnumerable<IBlockLine>) lines)
     {
         if (line is Instruction)
         {
             Instruction instruction = line as Instruction;
             
             if (instruction.Parameter == null)
                 il.Emit(instruction.Op);
             else if (instruction.Parameter is int)
                 il.Emit(instruction.Op, (int) instruction.Parameter);
             else if (instruction.Parameter is string)
                 il.Emit(instruction.Op, (string) instruction.Parameter);
             else if (instruction.Parameter is Type)
                 il.Emit(instruction.Op, (Type) instruction.Parameter);
             else if (instruction.Parameter is FieldInfo)
                 il.Emit(instruction.Op, (FieldInfo) instruction.Parameter);
             else if (instruction.Parameter is MethodInfo)
                 il.Emit(instruction.Op, (MethodInfo) instruction.Parameter);
             else if (instruction.Parameter is ConstructorInfo)
                 il.Emit(instruction.Op, (ConstructorInfo) instruction.Parameter);
             else if (instruction.Parameter is BlockLabel)
             {
                 BlockLabel blockLabel = (BlockLabel) instruction.Parameter;
                 
                 Label label;
                 
                 if (!labels.TryGetValue(blockLabel, out label))
                     throw new Exception("Label " + blockLabel.Name + " not marked");
                 
                 il.Emit(instruction.Op, label);
             }
             else if (instruction.Parameter is BlockLocal)
                 il.Emit(instruction.Op, locals[(BlockLocal) instruction.Parameter]);
             else
                 throw new NotImplementedException(instruction.Op.Value + " " + TypeNames.GetName(instruction.Parameter.GetType()));
         }
         else if (line is BlockInstruction)
         {
             BlockInstruction instruction = line as BlockInstruction;
             
             if (instruction.Operation == BlockOperation.Comment)
                 {}
             else if (instruction.Operation == BlockOperation.MarkLabel)
                 il.MarkLabel(labels[(BlockLabel) instruction.Parameter]);
             else if (instruction.Operation == BlockOperation.DeclareLocal)
             {
                 BlockLocal local = (BlockLocal) instruction.Parameter;
                 locals[local] = il.DeclareLocal(local.Type);
             }
             else if (instruction.Operation == BlockOperation.BeginScope)
                 il.BeginScope();
             else if (instruction.Operation == BlockOperation.EndScope)
                 il.EndScope();
             else
                 throw new Exception();
         }        
         else
             throw new Exception();
     }
 }
Example #45
0
 /// <summary>
 /// Calls a method on this variable
 /// </summary>
 /// <param name="Method">Method</param>
 /// <param name="Parameters">Parameters sent in</param>
 /// <returns>Variable returned by the function (if one exists, null otherwise)</returns>
 public virtual VariableBase Call(IMethodBuilder Method, object[] Parameters = null)
 {
     if (Method == null)
         throw new ArgumentNullException("Method");
     return Call((MethodBuilder)Method, Parameters);
 }
        private void WrapMethodBody(MethodSignature signature, IMethodBuilder bodyWriter)
        {
            WriteLine(signature.ToString(null, null));
            WriteLine("{");
            PushIndent("    ");

            Write(bodyWriter.GetMethodBody());

            PopIndent();
            WriteLine("}");
        }
 public ComparisonBytecodeGeneratingVisitor(IMethodBuilder methodBuilder, ITypeRef
     predicateClass)
 {
     _methodBuilder = methodBuilder;
     _predicateClass = predicateClass;
 }
Example #48
0
 /// <summary>
 /// Initializes the current instance
 /// with the <paramref name="source"/> container.
 /// </summary>
 /// <param name="source">The <see cref="IServiceContainer"/> instance that will hold the current instance.</param>
 public void Initialize(IServiceContainer source)
 {
     ProxyImplementor = (ITypeBuilder) source.GetService("ProxyImplementor", typeof (ITypeBuilder));
     MethodPicker = (IMethodPicker) source.GetService(typeof (IMethodPicker));
     ProxyMethodBuilder = (IMethodBuilder) source.GetService("ProxyMethodBuilder", typeof (IMethodBuilder));
 }
 /// <summary>
 /// Calls a method on this variable
 /// </summary>
 /// <param name="Method">Method</param>
 /// <param name="Parameters">Parameters sent in</param>
 /// <returns>Variable returned by the function (if one exists, null otherwise)</returns>
 public virtual VariableBase Call(IMethodBuilder Method, object[] Parameters = null)
 {
     Contract.Requires<ArgumentNullException>(Method!=null,"Method");
     return Call((MethodBuilder)Method, Parameters);
 }