mardi 21 mai 2019

Where will TypeBuilder.CreateType() stored, memory or filesystem?

Will TypeBuilder.CreateType() generate a local file or just keep it in memory? If it creates a local file, where will it be stored? I have seen files that sugests that it saves them to C:\Users[My Account]\AppData\Local\Temp but if I remove them they are not created again when running the code? I also read that the generated types are saved in the some folder as current executing assembly? But there is no generated files?

So where is it stored? If its even stored on disc?

Here is the code I use to create the type in runtime :

        private static Type CreateRaportType(List<PropertieInformation> propertieList, string className)
    {
        AssemblyName assemblyName;
        AssemblyBuilder assemblyBuilder;
        ModuleBuilder module;
        TypeBuilder typeBuilder;
        FieldBuilder field;
        PropertyBuilder property;
        MethodAttributes GetSetAttr;
        MethodBuilder currGetPropMthdBldr;
        MethodBuilder currSetPropMthdBldr;
        ILGenerator currGetIL;
        ILGenerator currSetIL;

        Type caType;
        CustomAttributeBuilder caBuilder;

        List<Object> objList = new List<object>();

        assemblyName = new AssemblyName();
        assemblyName.Name = "ReportAssembly";

        assemblyBuilder = Thread.GetDomain().DefineDynamicAssembly(assemblyName, AssemblyBuilderAccess.Run);
        module = assemblyBuilder.DefineDynamicModule("ReportModule");

        typeBuilder = module.DefineType(className, TypeAttributes.Public | TypeAttributes.Class, typeof(GeneratedClassBase));

        foreach (PropertieInformation propertieInfo in propertieList)
        {
            field = typeBuilder.DefineField("_" + propertieInfo.PropertieName, propertieInfo.PropertieType, FieldAttributes.Private);

            property = typeBuilder.DefineProperty(propertieInfo.PropertieName, PropertyAttributes.None, propertieInfo.PropertieType, new Type[] { propertieInfo.PropertieType });
            GetSetAttr = MethodAttributes.Public | MethodAttributes.HideBySig;

            currGetPropMthdBldr = typeBuilder.DefineMethod("get_value", GetSetAttr, propertieInfo.PropertieType, Type.EmptyTypes);

            currGetIL = currGetPropMthdBldr.GetILGenerator();
            currGetIL.Emit(OpCodes.Ldarg_0);
            currGetIL.Emit(OpCodes.Ldfld, field);
            currGetIL.Emit(OpCodes.Ret);

            currSetPropMthdBldr = typeBuilder.DefineMethod("set_value", GetSetAttr, null, new Type[] { propertieInfo.PropertieType });

            currSetIL = currSetPropMthdBldr.GetILGenerator();
            currSetIL.Emit(OpCodes.Ldarg_0);
            currSetIL.Emit(OpCodes.Ldarg_1);
            currSetIL.Emit(OpCodes.Stfld, field);
            currSetIL.Emit(OpCodes.Ret);

            // Last, we must map the two methods created above to our PropertyBuilder to
            // their corresponding behaviors, "get" and "set" respectively. 
            property.SetGetMethod(currGetPropMthdBldr);
            property.SetSetMethod(currSetPropMthdBldr);

            caType = typeof(Reportable);
            objList.Clear();
            objList.Add(propertieInfo.MemberToDataBind);
            objList.Add(propertieInfo.ControlToUse);
            objList.Add(propertieInfo.PropertieName);

            objList.Add(propertieInfo.PropertieInnerCollectionType);
            objList.Add(propertieInfo.PropertieInnerCollectionName);
            objList.Add(propertieInfo.DisplayName);
            objList.Add(-1);
            objList.Add(FieldListIcon.UnUsedItem);
            objList.Add(propertieInfo.SystemItemKey);
            objList.Add(null);

            var conInfo = caType.GetConstructor(Type.EmptyTypes);
            var conArgs = new object[] { };
            var caTypeFields = caType.GetFields();

            caBuilder = new CustomAttributeBuilder(conInfo, conArgs, caTypeFields, objList.ToArray());

            property.SetCustomAttribute(caBuilder);

            caType = typeof(DisplayNameAttribute);

            if (propertieInfo.IsList)
                caBuilder = new CustomAttributeBuilder(caType.GetConstructor(new Type[] { typeof(string) }), new string[] { propertieInfo.DisplayName });
            else
                caBuilder = new CustomAttributeBuilder(caType.GetConstructor(new Type[] { typeof(string) }), new string[] { propertieInfo.DisplayName });

            property.SetCustomAttribute(caBuilder);
        }
        return typeBuilder.CreateType();
    }





Aucun commentaire:

Enregistrer un commentaire