Пример #1
0
 public static PropertyDefOptions Create(ModuleDef module, UTF8String name, bool isInstance) => new PropertyDefOptions
 {
     Attributes  = 0,
     Name        = name,
     PropertySig = isInstance ?
                   PropertySig.CreateInstance(module.CorLibTypes.Int32) :
                   PropertySig.CreateStatic(module.CorLibTypes.Int32),
     Constant = null,
 };
Пример #2
0
        private static void WrapField(TypeDef type, string fieldName, string propertyName, string access)
        {
            FieldDef field             = type.Fields.First(f => f.Name == fieldName);
            var      propertySignature = PropertySig.CreateInstance(field.FieldType);
            var      property          = new PropertyDefUser(propertyName, propertySignature);

            type.Properties.Add(property);
            bool propertyValid = false;

            if (access.Contains("read"))
            {
                var           getterSignature = MethodSig.CreateInstance(field.FieldType);
                MethodDefUser getter          = new MethodDefUser("nuterra_get_" + propertyName, getterSignature, MethodAttributes.Public | MethodAttributes.HideBySig);
                getter.DeclaringType = type;
                getter.Body          = new CilBody();
                getter.Body.Instructions.Add(OpCodes.Ldarg_0.ToInstruction());
                getter.Body.Instructions.Add(OpCodes.Ldfld.ToInstruction(field));
                getter.Body.Instructions.Add(OpCodes.Ret.ToInstruction());
                property.GetMethod = getter;
                propertyValid      = true;
            }
            if (access.Contains("write"))
            {
                var           setterSignature = MethodSig.CreateInstance(type.Module.CorLibTypes.Void, field.FieldType);
                MethodDefUser setter          = new MethodDefUser("nuterra_set_" + propertyName, setterSignature, MethodAttributes.Public | MethodAttributes.HideBySig);
                setter.DeclaringType = type;
                setter.Body          = new CilBody();
                setter.Body.Instructions.Add(OpCodes.Ldarg_0.ToInstruction());
                setter.Body.Instructions.Add(OpCodes.Ldarg_1.ToInstruction());
                setter.Body.Instructions.Add(OpCodes.Stfld.ToInstruction(field));
                setter.Body.Instructions.Add(OpCodes.Ret.ToInstruction());
                property.SetMethod = setter;
                propertyValid      = true;
            }
            if (!propertyValid)
            {
                throw new ArgumentException("wrap_field must define read and/or write accessor", nameof(access));
            }
        }