Beispiel #1
0
        public ApiTypeBuilder Property(string name, Type propertyType, bool hasSetter = true, bool hasGetter = true, Type[] indexParameters = null, bool @static = false, bool setterInternal = false, bool getterInternal = false)
        {
            PropertyBuilder propertyBuilder = _typeBuilder.DefineProperty(name, PropertyAttributes.None, propertyType, indexParameters);

            if (hasGetter)
            {
                MethodAttributes visibility       = getterInternal ? MethodAttributes.Assembly : MethodAttributes.Public;
                MethodBuilder    getMethodBuilder = _typeBuilder.DefineMethod(string.Format("get_{0}", name), @static ? visibility | MethodAttributes.SpecialName | MethodAttributes.HideBySig | MethodAttributes.Static :
                                                                              visibility | MethodAttributes.SpecialName | MethodAttributes.HideBySig, propertyType, indexParameters);

                ILGenerator getIlGenerator = getMethodBuilder.GetILGenerator();
                getIlGenerator.Emit(ApiBuilderHelper.GetReturnOpCodeByType(propertyType));
                getIlGenerator.Emit(OpCodes.Ret);
                propertyBuilder.SetGetMethod(getMethodBuilder);
            }
            if (hasSetter)
            {
                MethodAttributes visibility       = setterInternal ? MethodAttributes.Assembly : MethodAttributes.Public;
                MethodBuilder    setMethodBuilder = _typeBuilder.DefineMethod(string.Format("set_{0}", name), @static ? visibility | MethodAttributes.SpecialName | MethodAttributes.HideBySig | MethodAttributes.Static :
                                                                              visibility | MethodAttributes.SpecialName | MethodAttributes.HideBySig, null, new[] { propertyType });

                ILGenerator setIlGenerator = setMethodBuilder.GetILGenerator();
                setIlGenerator.Emit(OpCodes.Ret);
                propertyBuilder.SetSetMethod(setMethodBuilder);
            }
            return(this);
        }
Beispiel #2
0
 public ApiTypeBuilder Build()
 {
     if (_ctor)
     {
         ConstructorBuilder constructorBuilder = _parent.TypeBuilder.DefineConstructor(_methodAttributes, CallingConventions.Standard, _parameters.Select(parameter => parameter.Type).ToArray());
         DefineParameters(constructorBuilder.DefineParameter);
         ILGenerator ilGenerator = constructorBuilder.GetILGenerator();
         ilGenerator.Emit(OpCodes.Ret);
     }
     else
     {
         MethodBuilder methodBuilder = _parent.TypeBuilder.DefineMethod(_name, _methodAttributes, _returnType, _parameters.Select(parameter => parameter.Type).ToArray());
         DefineParameters(methodBuilder.DefineParameter);
         ILGenerator ilGenerator = methodBuilder.GetILGenerator();
         ilGenerator.Emit(ApiBuilderHelper.GetReturnOpCodeByType(_returnType));
         ilGenerator.Emit(OpCodes.Ret);
     }
     return(_parent);
 }