public override void CreateMember(Generators.TypeGenerator generator) { System.Type returnType; if (ReturnType != null) { returnType = ReturnType.ResolveType(generator.Context); } else if (Body.ContainsNodeOfType <ReturnOrThrowStatement>(s => s.NodeType == StatementType.Return)) { returnType = TypeProvider.AnyType; } else { returnType = TypeProvider.VoidType; } var parameters = Parameters.Map(para => para.GetParameterInfo(generator.Context)); if (IsGetter || IsSetter) { CreateProperty(generator, returnType, parameters); } else { CreateFunction(generator, returnType, parameters); } }
public override void CreateMember(Generators.TypeGenerator generator) { System.Type baseType; if (BaseType != null) { baseType = BaseType.ResolveType(generator.Context); } else { baseType = typeof(FSObject); } var type = generator.DefineNestedType(Name, baseType, System.Reflection.TypeAttributes.Public); System.Type[] types = null; if (Implements != null) { types = Implements.Map(impl => impl.ResolveType(generator.Context)).AddLast(typeof(IFSObject)); } else { types = new System.Type[1] { typeof(IFSObject) }; } type.SetInterfaces(types); type.Source = Source; type.SetCustomAttribute(typeof(Runtime.RegisterAttribute), Utils.ReflectionHelpers.Register_Attr_Ctor, new object[] { Name }); foreach (var member in Members) { member.CreateMember(type); } generator.Add(type); }
private void CreateProperty(Generators.TypeGenerator generator, System.Type returnType, ParameterInfo[] parameters) { var parameterTypes = parameters.Map(p => p.Type); Generators.PropertyGenerator.PropertyHolder accessor = null; System.Type type = null; var name = string.Concat(char.ToUpper(Name.First()), Name.Substring(1)); var builder = generator.Builder; System.Reflection.MethodAttributes attributes = GetAttributes(); if (IsGetter) { type = returnType; string hiddenName = string.Concat("get_", name); if ((attributes & System.Reflection.MethodAttributes.Virtual) == System.Reflection.MethodAttributes.Virtual) { generator.CheckImplementMethod(Name, parameterTypes, ref hiddenName, ref returnType, ref attributes); } System.Reflection.Emit.MethodBuilder getBul = builder.DefineMethod(hiddenName, attributes, returnType, parameterTypes); accessor = new Generators.PropertyGenerator.PropertyHolder(Generators.PropertyType.Get, new Generators.MethodGenerator(getBul, parameters, generator) { SyntaxBody = Body }); } if (IsSetter) { type = parameterTypes.FirstOrDefault(); accessor = new Generators.PropertyGenerator.PropertyHolder(Generators.PropertyType.Set, new Generators.MethodGenerator(builder.DefineMethod(string.Concat("set_", name), attributes, returnType, parameterTypes), parameters, generator) { SyntaxBody = Body }); } if (generator.TryGetProperty(Name, out Generators.PropertyGenerator property) == false) { var pb = generator.Builder.DefineProperty(name, System.Reflection.PropertyAttributes.None, type, null); property = new Generators.PropertyGenerator(generator, pb); property.SetCustomAttribute(typeof(Runtime.RegisterAttribute), Utils.ReflectionHelpers.Register_Attr_Ctor, new[] { Name }); generator.Add(property); } System.Reflection.Emit.PropertyBuilder propertyBuilder = property.GetBuilder(); if (IsGetter) { propertyBuilder.SetGetMethod(accessor.Method.GetBuilder()); } else if (IsSetter) { propertyBuilder.SetSetMethod(accessor.Method.GetBuilder()); } else { throw new System.Exception("Accessor not found"); } property.Accessors.Add(accessor); }
public override void CreateMember(Generators.TypeGenerator generator) { System.Reflection.FieldAttributes attrs = GetAttribute(); foreach (var field in Declarations) { Generators.FieldGenerator fieldGen = new Generators.FieldGenerator(generator, attrs, field); fieldGen.SetCustomAttribute(typeof(Runtime.RegisterAttribute), Utils.ReflectionHelpers.Register_Attr_Ctor, new[] { field.Name }); generator.Add(fieldGen); } }
private void CreateFunction(Generators.TypeGenerator generator, System.Type returnType, ParameterInfo[] parameters) { var parameterTypes = parameters.Map(p => p.Type); //todo override toString and others var name = string.Concat(char.ToUpper(Name.First()), Name.Substring(1)); System.Reflection.MethodAttributes attributes = GetAttributes(); if ((attributes & System.Reflection.MethodAttributes.Virtual) == System.Reflection.MethodAttributes.Virtual) { generator.CheckImplementMethod(Name, parameterTypes, ref name, ref returnType, ref attributes); } // create method var method = generator.Builder.DefineMethod(name, attributes, returnType, parameterTypes); //set runtime method name Generators.MethodGenerator methodGen = new Generators.MethodGenerator(method, parameters, generator) { SyntaxBody = Body }; methodGen.SetCustomAttribute(typeof(Runtime.RegisterAttribute), Utils.ReflectionHelpers.Register_Attr_Ctor, new object[] { Name }); generator.Add(methodGen); }