protected override string DoGenerateValueCSharp(IDynamicAssemblyBuilder dynamicAssemblyBuilder)
        {
            var cSharpCode = new StringBuilder();

            if (_createInstanceMethodName != null)
            {
                var dynamicImplementationsClass = dynamicAssemblyBuilder.GetDynamicallyGeneratedClass(DynamicCodeGenerationHelpers.DynamicImplementationsClassName);
                cSharpCode.Append($"{dynamicImplementationsClass.ClassFullName}.{_createInstanceMethodName}()");
            }
            else
            {
                AddConstructorCode(dynamicAssemblyBuilder, cSharpCode);
            }

            return(cSharpCode.ToString());
        }
Ejemplo n.º 2
0
        public string GenerateValueCSharp(ClassMemberData classMemberData, IDynamicAssemblyBuilder dynamicAssemblyBuilder)
        {
            var valueCSharpStrBldr = new StringBuilder();

            if (classMemberData.IsInjectedClassMember)
            {
                var dynamicallyGeneratedClass = dynamicAssemblyBuilder.GetDynamicallyGeneratedClass(DynamicCodeGenerationHelpers.ClassMembersClassName);

                if (!_injectedTypesToVariableNameCSharpFile.TryGetValue(classMemberData.ClassInfo.Type, out var variableName))
                {
                    variableName = $"{classMemberData.ClassInfo.Type.Name}_{GlobalsCoreAmbientContext.Context.GenerateUniqueId()}";

                    _injectedTypesToVariableNameCSharpFile[classMemberData.ClassInfo.Type] = variableName;

                    var variableInitializationCode = new StringBuilder();

                    variableInitializationCode.AppendFormat("public static {0} {1}=", classMemberData.ClassInfo.TypeCSharpFullName, variableName);

#pragma warning disable CS0612, CS0618
                    variableInitializationCode.Append($"{typeof(DiContainerBuilderConfiguration).FullName}.{nameof(DiContainerBuilderConfiguration.DiContainerStatic)}.{nameof(IDiContainer.Resolve)}");
#pragma warning restore CS0612, CS0618

                    variableInitializationCode.Append($"<{classMemberData.ClassInfo.TypeCSharpFullName}>();");

                    dynamicallyGeneratedClass.AddCodeLine(variableInitializationCode.ToString());
                }

                valueCSharpStrBldr.Append($"{dynamicallyGeneratedClass.ClassFullName}.{variableName}");
            }
            else
            {
                valueCSharpStrBldr.Append($"{classMemberData.ClassInfo.TypeCSharpFullName}");
            }

            // Add class member name
            valueCSharpStrBldr.Append($".{classMemberData.ClassMemberInfo.Name}");

            if (classMemberData.ClassMemberCategory == ClassMemberCategory.Method)
            {
                valueCSharpStrBldr.Append("(");
                valueCSharpStrBldr.Append(string.Join(",", classMemberData.Parameters.Select(x => x.GenerateValueCSharp(dynamicAssemblyBuilder))));
                valueCSharpStrBldr.Append(")");
            }


            return(valueCSharpStrBldr.ToString());
        }
        protected override void AddCodeOnGenerateValueCSharp(IDynamicAssemblyBuilder dynamicAssemblyBuilder)
        {
            List <IInjectedPropertyElement> injectedProperties;

            if (InjectedProperties == null)
            {
                injectedProperties = new List <IInjectedPropertyElement>();
            }
            else
            {
                injectedProperties = new List <IInjectedPropertyElement>(InjectedProperties.AllProperties);
            }

            if (injectedProperties.Count > 0)
            {
                var dynamicImplementationsClass = dynamicAssemblyBuilder.GetDynamicallyGeneratedClass(DynamicCodeGenerationHelpers.DynamicImplementationsClassName);

                var createInstanceMethodData = dynamicImplementationsClass.StartMethod("CreateInstance", ValueTypeInfo.Type, new IMethodParameterInfo[0], AccessLevel.Public, true, true);
                _createInstanceMethodName = createInstanceMethodData.MethodName;

                createInstanceMethodData.AddCodeLine("{");

                createInstanceMethodData.AddCode("var instance=");

                var cSharpCode = new StringBuilder();
                AddConstructorCode(dynamicAssemblyBuilder, cSharpCode);
                createInstanceMethodData.AddCode(cSharpCode.ToString());

                createInstanceMethodData.AddCodeLine(";");

                foreach (var injectedProperty in injectedProperties)
                {
                    createInstanceMethodData.AddCodeLine($"instance.{injectedProperty.Name}={injectedProperty.GenerateValueCSharp(dynamicAssemblyBuilder)};");
                }

                createInstanceMethodData.AddCodeLine("return instance;");

                createInstanceMethodData.AddCodeLine("}");
            }
        }
Ejemplo n.º 4
0
        protected override string DoGenerateValueCSharp(IDynamicAssemblyBuilder dynamicAssemblyBuilder)
        {
            var dynamicallyGeneratedClass = dynamicAssemblyBuilder.GetDynamicallyGeneratedClass(GetSettingsClassName());

            return($"{dynamicallyGeneratedClass.ClassFullName}.{DynamicCodeGenerationHelpers.GetSettingValuePropertyName(Name)}");
        }
Ejemplo n.º 5
0
        protected override void AddCodeOnGenerateValueCSharp(IDynamicAssemblyBuilder dynamicAssemblyBuilder)
        {
            var dynamicallyGeneratedClass = dynamicAssemblyBuilder.GetDynamicallyGeneratedClass(GetSettingsClassName());

            dynamicallyGeneratedClass.AddCodeLine($"public static {ValueTypeInfo.TypeCSharpFullName} {DynamicCodeGenerationHelpers.GetSettingValuePropertyName(Name)} {{ get; }} = {DecoratedValueInitializerElement.GenerateValueCSharp(dynamicAssemblyBuilder)};");
        }