Exemplo n.º 1
0
        internal static T WithFieldInternal <T>(this T member, string name, SystemTypeOrTypeReference fieldType, FieldAttributes?attributes = null)
            where T : IMemberDefinition
        {
            if (name == null)
            {
                throw new ArgumentNullException(nameof(name));
            }
            if (fieldType == null)
            {
                throw new ArgumentNullException(nameof(fieldType));
            }

            var field = new FieldDefinition(name, attributes ?? FieldAttributes.FamORAssem | FieldAttributes.Public, fieldType.GetTypeReference(member.GetModule()));

            var definition = member as TypeDefinition;

            if (definition != null)
            {
                definition.Resolve().Fields.Add(field);
            }
            else
            {
                member.DeclaringType.Fields.Add(field);
            }

            return(member);
        }
Exemplo n.º 2
0
        public FluentEmitter WithVariable(SystemTypeOrTypeReference varType, string name = null)
        {
            var var = new VariableDefinition(varType.GetTypeReference(Module));

            if (!string.IsNullOrEmpty(name))
            {
                var.Name = name;
            }
            Variables.Add(var);
            return(this);
        }
Exemplo n.º 3
0
        public static MethodDefinition WithVariable(this MethodDefinition method, SystemTypeOrTypeReference varType, string name = null)
        {
            var var = new VariableDefinition(varType.GetTypeReference(method.GetModule()));

            if (!string.IsNullOrEmpty(name))
            {
                var.Name = name;
            }
            method.Body.Variables.Add(var);
            return(method);
        }
Exemplo n.º 4
0
        public FluentEmitter NewObj(SystemTypeOrTypeReference type, params SystemTypeOrTypeReference[] paramtypes)
        {
            // todo: generic and base constructors don't work currently

            // todo: newobj for primitives should emit initobj and not throw any exception
            if (type.GetTypeReference(Module).IsPrimitive)
            {
                throw new Exception("primitive value types like int, bool, long .. don't have a constructor. newobj instruction not possible");
            }

            var constructors = type.GetTypeReference(Module).Resolve().GetConstructors()
                               .Where(c => AreParameterListsEqual(c.Parameters.Select(p => p.ParameterType), paramtypes.Select(p => p.GetTypeReference(Module)))).ToList();

            if (constructors.Count() != 1)
            {
                throw new Exception("Can not find constructor"); // todo: better exception info, ncrunch: no coverage
            }
            return(Emit(OpCodes.Newobj, constructors.First()));
        }
 public FluentEmitter Emit(OpCode opcode, SystemTypeOrTypeReference arg)
 {
     return(Emit(Instruction.Create(opcode, arg.GetTypeReference(Module))));
 }
		public FluentEmitter Emit(OpCode opcode, SystemTypeOrTypeReference arg)
		{
			return Emit(Instruction.Create(opcode, arg.GetTypeReference(Module)));
		}
Exemplo n.º 7
0
        public static MethodDefinition CreateMethod(this TypeDefinition type, string name = null, SystemTypeOrTypeReference returnType = null, MethodAttributes?attributes = null)
        {
            var module = type.GetModule();
            var t      = returnType != null?returnType.GetTypeReference(type.GetModule()) : module.TypeSystem.Void;

            var method = new MethodDefinition(name ?? Generate.Name.ForMethod(), attributes ?? 0, t);

            var definition = type as TypeDefinition;

            if (definition != null)
            {
                definition.Resolve().Methods.Add(method);
            }
            else
            {
                type.DeclaringType.Methods.Add(method);
            }

            return(method);
        }
Exemplo n.º 8
0
 public static MethodDefinition CreateMethod(this TypeDefinition type, SystemTypeOrTypeReference returnType, MethodAttributes?attributes = null)
 {
     return(CreateMethod(type, null, returnType.GetTypeReference(type.GetModule()), attributes));
 }