private static Type BuildMetaInfoType(ModuleBuilder moduleBuilder, ITypeDescription type)
        {
            var typeBuilder = moduleBuilder.DefineType(MetaDataNameSpace + "." + CodeGenerationUtils.ClassName(type.TypeName), CodeGenerationUtils.PublicClass());

            AddNameProperty(typeBuilder, type);
            return(typeBuilder.CreateType());
        }
 private MethodAttributes QueryPropertyAccessRights(TypeBuilder parentNS)
 {
     if (alwaysInstanceMethod)
     {
         return(CodeGenerationUtils.PublicGetter());
     }
     return(parentNS == rootType?CodeGenerationUtils.StaticPublicGetter() : CodeGenerationUtils.PublicGetter());
 }
        static MethodBuilder CreateNewInstanceGetter(TypeBuilder typeBuilder, PropertyBuilder property,
                                                     ConstructorInfo constructor, MethodAttributes accessRights)
        {
            var getterMethod = CodeGenerationUtils.GetterMethodFor(typeBuilder, property, accessRights);

            CodeGenerationUtils.ReturnNewInstanceILInstructions(constructor, getterMethod);
            return(getterMethod);
        }
        private void CreateMetaDataGetter(MethodBuilder getterMethod,
                                          Type type,
                                          ITypeDescription typeDescription)
        {
            var returnType = getterMethod.ReturnType;

            CodeGenerationUtils.ReturnNewInstanceILInstructions(
                returnType.GetConstructors().Single(), getterMethod);
        }
        private string NameOfProperty(ITypeDescription typeDescription)
        {
            var name = typeDescription.TypeName.NameWithGenerics.Split('.').Last().Replace('+', '_');

            if (needsAssemblyInName.Contains(typeDescription))
            {
                return(name + "_" + CodeGenerationUtils.NormalizedAssemblyName(typeDescription.TypeName));
            }
            return(name);
        }
        private Type CreateMetaInfoProperty(IEnumerable <ByNameGrouping> typesGroupesByName)
        {
            var typeBuilder = moduleBuilder.DefineType(MetaDataClassName, CodeGenerationUtils.PublicClass());

            var finalType = new NamespaceContextGenerator("MetaData", moduleBuilder, typeBuilder, typesGroupesByName,
                                                          CreateMetaDataGetter,
                                                          (t, d) => BuildMetaInfoType(moduleBuilder, d), true).BuildType();

            return(finalType.CreateType());
        }
        private static void AddLabelProperty(TypeBuilder typeBuilder, string propertyName,
                                             string valueToReturn)
        {
            var property      = CodeGenerationUtils.DefineProperty(typeBuilder, propertyName, typeof(string));
            var methodBuilder = CodeGenerationUtils.GetterMethodFor(typeBuilder, property, CodeGenerationUtils.PublicGetter());

            var ilCode = methodBuilder.GetILGenerator();

            ilCode.Emit(OpCodes.Ldstr, valueToReturn);
            ilCode.Emit(OpCodes.Ret);
        }
 private void CreateEntries(ByNameGrouping type)
 {
     foreach (var typeToBuildFor in type.Members)
     {
         var definePropertyOn = FindLocationForProperty(type, typeToBuildFor.Key);
         var querableType     = propertyTypeBuilder(typeToBuildFor.Value, typeToBuildFor.Key);
         var nameOfProperty   = NameOfProperty(typeToBuildFor.Key);
         var property         = CodeGenerationUtils.DefineProperty(definePropertyOn, nameOfProperty, querableType);
         var getterMethod     = CodeGenerationUtils.GetterMethodFor(definePropertyOn, property, QueryPropertyAccessRights(definePropertyOn));
         buildPropertyOnType(getterMethod, typeToBuildFor.Value,
                             typeToBuildFor.Key);
     }
 }
        private void CreateMetaDataStructure(IEnumerable <ByNameGrouping> typesGroupesByName)
        {
            var metaInfoType = CreateMetaInfoProperty(typesGroupesByName);
            var constructorOfMetaInfoType = metaInfoType.GetConstructors().Single();

            var property = CodeGenerationUtils.DefineProperty(rootType, MetaDataProperty, metaInfoType);

            var getterMethod = CodeGenerationUtils.GetterMethodFor(rootType, property, CodeGenerationUtils.StaticPublicGetter());
            var lambda       = Expression.Lambda(typeof(Func <>).MakeGenericType(metaInfoType),
                                                 Expression.New(constructorOfMetaInfoType), new ParameterExpression[0]);

            lambda.CompileToMethod(getterMethod);
        }
        internal static Type CreateContextType(ModuleBuilder builder,
                                               IEnumerable <KeyValuePair <ITypeDescription, Type> > types)
        {
            var typesGroupesByName = from t in types
                                     where t.Key.IsBusinessEntity
                                     group t by t.Key.Name
                                     into byName
                                     select new ByNameGrouping(byName.Key, byName);
            var typeBuilder = builder.DefineType(QueryContextClassName,
                                                 CodeGenerationUtils.PublicClass(), typeof(ContextRoot));

            return(new ContextTypeGenerator(builder, typeBuilder).Build(typesGroupesByName));
        }
        private TypeBuilder BuildNamespaceContext(string lastNameSpace,
                                                  string forNamespace, string ns)
        {
            var parentNS = nameSpaceHolder.TryGet(lastNameSpace).GetValue(rootType);

            var currentType = BuildNSContextType(forNamespace);

            var property = CodeGenerationUtils.DefineProperty(parentNS, ns, currentType.Item1);

            property.SetGetMethod(CreateNewInstanceGetter(parentNS, property, currentType.Item2, QueryPropertyAccessRights(parentNS)));

            nameSpaceHolder[forNamespace] = currentType.Item1;
            return(currentType.Item1);
        }
        private Tuple <TypeBuilder, ConstructorBuilder> BuildNSContextType(string forNamespace)
        {
            var currentType =
                moduleBuilder.DefineType(
                    CodeGenerator.NameSpace + "." + forNamespace + "." + nameSpaceClassesPrefix + "NameSpaceContext",
                    CodeGenerationUtils.PublicClass());
            var constructor = currentType.DefineConstructor(CodeGenerationUtils.PublicConstructurSignature(),
                                                            CallingConventions.Standard, new Type[0]);
            var ilGenerator = constructor.GetILGenerator();

            ilGenerator.Emit(OpCodes.Ldarg_0);
            ilGenerator.Emit(OpCodes.Call, typeof(object).GetConstructors().Single());
            ilGenerator.Emit(OpCodes.Ret);
            return(Tuple.Create(currentType, constructor));
        }
Exemple #13
0
 private static string BuildName(TypeName className)
 {
     return(NameSpace + "." + CodeGenerationUtils.ClassName(className));
 }