Beispiel #1
0
        public ConstructorBuilder CreateConstructor(TypeBuilder type, Type[] args, ExecBld body)
        {
            var cnstr = type.DefineConstructor(MethodAttributes.Public, CallingConventions.Standard, args);

            body.Build(cnstr.GetILGenerator());
            return(cnstr);
        }
Beispiel #2
0
        public void AddProperty(TypeBuilder type, PropertyInfo t, ExecBld get, ExecBld set, Type prototype)
        {
            var prop         = type.DefineProperty(t.Name, PropertyAttributes.None, t.PropertyType, Type.EmptyTypes);
            var propSettings = MethodAttributes.Virtual;

            var getMeth = type.DefineMethod("get_" + t.Name, propSettings, t.PropertyType, Type.EmptyTypes);

            get.Build(getMeth.GetILGenerator());
            prop.SetGetMethod(getMeth);
            type.DefineMethodOverride(getMeth, prototype.GetMethod(getMeth.Name));

            var setMeth = type.DefineMethod("set_" + t.Name, propSettings, null, new[] { t.PropertyType });

            set.Build(setMeth.GetILGenerator());
            prop.SetSetMethod(setMeth);
            type.DefineMethodOverride(setMeth, prototype.GetMethod(setMeth.Name));
        }
Beispiel #3
0
 public static ExecBld Cast(ExecBld src, Type tSrc, Type tDst)
 {
     if (!tSrc.IsSubclassOf(typeof(ValueType)) && tDst.IsSubclassOf(typeof(ValueType)))
     {
         return(new ExecBld(new ExecBldItem(OpCodes.Unbox_Any, tDst), src.m_items));
     }
     else if (!tSrc.IsSubclassOf(typeof(ValueType)) && !tDst.IsSubclassOf(typeof(ValueType)))
     {
         return(new ExecBld(new ExecBldItem(OpCodes.Castclass, tDst), src.m_items));
     }
     else if (tSrc.IsSubclassOf(typeof(ValueType)) && !tDst.IsSubclassOf(typeof(ValueType)))
     {
         return(new ExecBld(new ExecBldItem(OpCodes.Box, tSrc), src.m_items));
     }
     else
     {
         return(Cast(Cast(src, tSrc, typeof(object)), typeof(object), tDst));
     }
 }
Beispiel #4
0
        public PropertyBuilder AddProperty(TypeBuilder type, string propertyName, Type propertyType)
        {
            var fieldName    = "_" + propertyName;
            var field        = type.DefineField(fieldName, propertyType, FieldAttributes.Private);
            var prop         = type.DefineProperty(propertyName, PropertyAttributes.HasDefault, propertyType, null);
            var propSettings = MethodAttributes.Public | MethodAttributes.SpecialName | MethodAttributes.HideBySig;

            var getMeth = type.DefineMethod("get_" + propertyName, propSettings, propertyType, Type.EmptyTypes);
            var get     = ExecBld.Return(ExecBld.GetFld(field, ExecBld.GetThis()));

            get.Build(getMeth.GetILGenerator());
            prop.SetGetMethod(getMeth);

            var setMeth = type.DefineMethod("set_" + propertyName, propSettings, null, new Type[] { propertyType });
            var set     = ExecBld.Return(ExecBld.SetFld(field, ExecBld.GetThis(), ExecBld.GetArg(1)));

            set.Build(setMeth.GetILGenerator());
            prop.SetSetMethod(setMeth);

            return(prop);
        }
Beispiel #5
0
        public void CreateMethod(TypeBuilder type, string name, Type returnType, Type[] argType, Type interf, ExecBld body)
        {
            var equelMeth = type.DefineMethod(name, MethodAttributes.Virtual, returnType, argType);

            body.Build(equelMeth.GetILGenerator());
            if (interf != null)
            {
                type.DefineMethodOverride(equelMeth, interf.GetMethod(name));
            }
        }
Beispiel #6
0
 public static ExecBld SetProp(PropertyInfo prop, ExecBld src, ExecBld value)
 {
     return(new ExecBld(new ExecBldItem(OpCodes.Callvirt, prop.GetSetMethod(true)), src.m_items.Union(value.m_items)));
 }
Beispiel #7
0
 public static ExecBld GetProp(PropertyBuilder prop, ExecBld src)
 {
     return(new ExecBld(new ExecBldItem(OpCodes.Callvirt, prop.GetGetMethod(true)), src.m_items));
 }
Beispiel #8
0
 public static ExecBld SetFld(FieldBuilder field, ExecBld src, ExecBld value)
 {
     return(new ExecBld(new ExecBldItem(OpCodes.Stfld, field), src.m_items.Union(value.m_items)));
 }
Beispiel #9
0
 public static ExecBld GetFld(FieldBuilder field, ExecBld src)
 {
     return(new ExecBld(new ExecBldItem(OpCodes.Ldfld, field), src.m_items));
 }
Beispiel #10
0
 public static ExecBld Return(ExecBld src)
 {
     return(new ExecBld(new ExecBldItem(OpCodes.Ret), src.m_items));
 }
Beispiel #11
0
 public static ExecBld SetItem <T>(ExecBld src, ExecBld index, ExecBld value)
 {
     return(new ExecBld(new ExecBldItem(OpCodes.Stelem, typeof(T)), src.m_items.Union(index.m_items).Union(value.m_items)));
 }
Beispiel #12
0
 public static ExecBld GetItem <T>(ExecBld src, ExecBld index)
 {
     return(new ExecBld(new ExecBldItem(OpCodes.Ldelem, typeof(T)), src.m_items.Union(index.m_items)));
 }
Beispiel #13
0
 public static ExecBld SetLoc(int index, ExecBld value)
 {
     return(new ExecBld(new ExecBldItem(OpCodes.Stloc, index), value.m_items));
 }