Esempio n. 1
0
        private TypeDefinition <T> CreateTypeDefinition <T>(string dataSetName, IFactory <T> dataContractFactory) where T : class
        {
            var typedTypeDefinition = new TypeDefinition <T>(_defaultFactory, _errorReporter).Initialize(dataSetName, dataContractFactory);
            var hasMappings         = false;

            // Add mappings for fields that have [Mapping] attributes attached
            foreach (var property in typeof(T).GetProperties())
            {
                foreach (var mappingAttribute in property
                         .GetCustomAttributes(true)
                         .Select(a => a as MappingAttribute)
                         .Where(a => a != null))
                {
                    if (mappingAttribute.DefaultValue != null && !property.PropertyType.IsInstanceOfType(mappingAttribute.DefaultValue))
                    {
                        throw new PriusException("The default value in the mapping attribute must match the type of property the mapping references. Field: " + mappingAttribute.FieldName + ", Type: " + typeof(T).FullName);
                    }

                    typedTypeDefinition.AddField(mappingAttribute.FieldName, property, mappingAttribute.DefaultValue);
                    hasMappings = true;
                }
            }

            // Apply any custom mapping behaviour
            if (typeof(IDataContract <T>).IsAssignableFrom(typeof(T)))
            {
                var dataContract = typedTypeDefinition.Create() as IDataContract <T>;
                try
                {
                    dataContract.AddMappings(typedTypeDefinition, dataSetName);
                    hasMappings = true;
                }
                finally
                {
                    var disposable = dataContract as IDisposable;
                    disposable?.Dispose();
                }
            }

            if (!hasMappings)
            {
                foreach (var property in typeof(T).GetProperties())
                {
                    var type = property.PropertyType;
                    if (type.IsAbstract || type.IsArray || type.IsGenericType || type.IsNotPublic)
                    {
                        continue;
                    }

                    var defaultValue = type.IsValueType ? Activator.CreateInstance(type) : null;
                    typedTypeDefinition.AddField(property.Name, property, defaultValue);
                }
            }

            return(typedTypeDefinition);
        }
        public static FieldDefinition CreateLockObject(this TypeDefinition type)
        {
            FieldDefinition field = type.AddField <object>($"ALE__GENERATED__{type.Name}__LOCK", FieldAttributes.Private);

            MethodDefinition ctor = type.GetConstructor();
            ILProcessor      il   = ctor.BeginEdit();

            Instruction newObj   = Instruction.Create(OpCodes.Newobj, type.Module.ImportReference(typeof(object).GetConstructor(Array.Empty <Type>())));
            Instruction setField = Instruction.Create(OpCodes.Stfld, field);

            il.InsertAfter(il.Body.Instructions[0], newObj);
            il.InsertAfter(il.Body.Instructions[1], setField);
            il.InsertAfter(il.Body.Instructions[2], Instruction.Create(OpCodes.Ldarg_0));

            ctor.EndEdit();

            return(field);
        }
Esempio n. 3
0
        private static PropertyDefinition CreateProperty <T>(TypeDefinition wrapper, ModuleDefinition module, string name)
        {
            FieldDefinition field = wrapper.AddField <T>($"<{name}>k__BackingField", FieldAttributes.Private);

            field.CustomAttributes.Add(new CustomAttribute(module.GetConstructor <CompilerGeneratedAttribute>()));

            var get = wrapper.AddMethod <T>($"get_{name}",
                                            MethodAttributes.Public | MethodAttributes.Final | MethodAttributes.HideBySig | MethodAttributes.SpecialName | MethodAttributes.NewSlot | MethodAttributes.Virtual);

            get.AddAttribute <CompilerGeneratedAttribute>();
            get.AddAttribute <IsReadOnlyAttribute>();

            ILProcessor il = get.BeginEdit();

            il.EmitLdarg();
            il.Emit(OpCodes.Ldfld, field);
            il.Emit(OpCodes.Ret);

            get.EndEdit();

            var set       = wrapper.AddMethod($"set_{name}", MethodAttributes.Public | MethodAttributes.HideBySig | MethodAttributes.SpecialName);
            var paraValue = set.AddParameter <T>("value");

            set.AddAttribute <CompilerGeneratedAttribute>();

            il = set.BeginEdit();

            il.EmitLdarg();
            il.EmitLdarg(paraValue);
            il.Emit(OpCodes.Stfld, field);
            il.Emit(OpCodes.Ret);

            set.EndEdit();

            PropertyDefinition property = new PropertyDefinition(name, PropertyAttributes.None, module.GetTypeReference <T>())
            {
                GetMethod = get,
                SetMethod = set
            };

            wrapper.Properties.Add(property);

            return(property);
        }
        public MethodDefinition InjectNestedCommandClass(TypeDefinition type)
        {
            var name        = string.Format("<>__NestedCommandImplementationFor" + Command.CommandName);
            var commandType = new TypeDefinition(type.Namespace, name, DefaultTypeNAttributesForCommand)
            {
                BaseType = Assets.TypeReferences.Object
            };

            var field = commandType.AddField(type, OwnerFieldName);

            field.IsInitOnly = true;
            //field = commandType.AddField(Assets.TypeReferences.EventHandler, CanExecuteChangedFieldName);

            ImplementICommandInterface(commandType);

            var ctor = CreateConstructor(commandType);

            commandType.Methods.Add(ctor);
            type.NestedTypes.Add(commandType);
            return(ctor);
        }
Esempio n. 5
0
            private TypeDefinition <T> CreateTypeDefinition <T>(string dataSetName) where T : class
            {
                var typedTypeDefinition = new TypeDefinition <T>().Initialize(dataSetName);

                // Add mappings for fields that have [Mapping] attributes attached
                foreach (var property in typeof(T).GetProperties())
                {
                    foreach (var mappingAttribute in property
                             .GetCustomAttributes(true)
                             .Select(a => a as MappingAttribute)
                             .Where(a => a != null))
                    {
                        if (mappingAttribute.DefaultValue != null && !property.PropertyType.IsInstanceOfType(mappingAttribute.DefaultValue))
                        {
                            throw new PriusException("The default value in the mapping attribute must match the type of property the mapping references. Field: " + mappingAttribute.FieldName + ", Type: " + typeof(T).FullName);
                        }

                        typedTypeDefinition.AddField(mappingAttribute.FieldName, property, mappingAttribute.DefaultValue);
                    }
                }

                // Apply any custom mapping behaviour
                if (typeof(IDataContract <T>).IsAssignableFrom(typeof(T)))
                {
                    var dataContract = typedTypeDefinition.Create() as IDataContract <T>;
                    try
                    {
                        dataContract.AddMappings(typedTypeDefinition, dataSetName);
                    }
                    finally
                    {
                        var disposable = dataContract as IDisposable;
                        disposable?.Dispose();
                    }
                }

                return(typedTypeDefinition);
            }
 public static FieldDefinition AddField <T>(this TypeDefinition type, string name, FieldAttributes attributes)
 {
     return(type.AddField(name, attributes, type.Module.GetTypeReference <T>()));
 }
Esempio n. 7
0
        public static MethodDefinition CreateAddEventMethod(this TypeDefinition targetType, string eventName,
                                                            TypeReference eventType, Assets assets, Action <ILProcessor> methodBodyWriter = null)
        {
            var method = new MethodDefinition("add_" + eventName,
                                              MethodAttributes.Final | MethodAttributes.HideBySig | MethodAttributes.SpecialName | MethodAttributes.NewSlot |
                                              MethodAttributes.Virtual | MethodAttributes.Public,
                                              assets.TypeReferences.Void)
            {
                Body = { InitLocals = true }
            };

            var eventHandlerParameter = new ParameterDefinition("value", ParameterAttributes.None, assets.TypeReferences.EventHandler);

            method.Parameters.Add(eventHandlerParameter);
            var il = method.Body.GetILProcessor();

            if (methodBodyWriter == null)
            {
                var varDef0 = new VariableDefinition(eventType);
                method.Body.Variables.Add(varDef0);
                var varDef1 = new VariableDefinition(eventType);
                method.Body.Variables.Add(varDef1);
                var varDef2 = new VariableDefinition(eventType);
                method.Body.Variables.Add(varDef2);
                var varDef3 = new VariableDefinition(assets.TypeReferences.Boolean);
                method.Body.Variables.Add(varDef3);
                var field = targetType.Fields.FirstOrDefault(fld => fld.Name == eventName);
                if (field == null)
                {
                    assets.Log.LogInfo(string.Format("Adding field {0} to {1} in CreateAddEventMethod.",
                                                     eventName,
                                                     targetType.FullName));
                    field = targetType.AddField(eventType, eventName);
                }
                Instruction loopStart;
                il.Append(il.Create(OpCodes.Nop));
                il.Append(il.Create(OpCodes.Ldarg_0));
                il.Append(il.Create(OpCodes.Ldfld, field));
                il.Append(il.Create(OpCodes.Stloc_0));
                il.Append(loopStart = il.Create(OpCodes.Ldloc_0));
                il.Append(il.Create(OpCodes.Stloc_1));
                il.Append(il.Create(OpCodes.Ldloc_1));
                il.Append(il.Create(OpCodes.Ldarg_1));
                il.Append(il.Create(OpCodes.Call, assets.DelegateCombineMethodReference));
                il.Append(il.Create(OpCodes.Castclass, eventType));
                il.Append(il.Create(OpCodes.Stloc_2));
                il.Append(il.Create(OpCodes.Ldarg_0));
                il.Append(il.Create(OpCodes.Ldflda, field));
                il.Append(il.Create(OpCodes.Ldloc_2));
                il.Append(il.Create(OpCodes.Ldloc_1));
                il.Append(il.Create(OpCodes.Call, assets.InterlockedCompareExchangeOfEventHandler));
                il.Append(il.Create(OpCodes.Stloc_0));
                il.Append(il.Create(OpCodes.Ldloc_0));
                il.Append(il.Create(OpCodes.Ldloc_1));
                il.Append(il.Create(OpCodes.Ceq));
                il.Append(il.Create(OpCodes.Ldc_I4_0));
                il.Append(il.Create(OpCodes.Ceq));
                il.Append(il.Create(OpCodes.Stloc_3));
                il.Append(il.Create(OpCodes.Ldloc_3));
                il.Append(il.Create(OpCodes.Brtrue_S, loopStart));
                il.Append(il.Create(OpCodes.Ret));
            }
            else
            {
                methodBodyWriter(il);
            }
            return(method);
        }
Esempio n. 8
0
 static public FieldDefinition CreateField(this TypeDefinition item, TypeReference type, string name, FieldAttributes attributes, IEnumerable <Instruction> instructions = null)
 {
     return(item.AddField(new FieldDefinition(name, attributes, type), instructions));
 }