public void WriteDeclarations(ICodeWriter declSpace)
 {
     declSpace.GeneratedByComment();
     foreach (var(t, (idx, opt)) in _mapping)
     {
         if (idx != 1)
         {
             declSpace.Append("var s").Append(idx)
             .Append(" = scope.ServiceProvider.GetService<").AppendCSharpName(t, true, true, true).Append(">( ").Append(!opt).Append(" );").NewLine();
         }
     }
     foreach (var(name, def) in _parametersArray)
     {
         declSpace.Append("var ").Append(name).Append(" = new object[]{ ").Append(def).Append(" };").NewLine();
     }
 }
Exemple #2
0
        internal static ICodeWriter DoAppendSignature(
            this ICodeWriter @this,
            AccessProtectionOption protection,
            string frontModifier,
            MethodInfo method)
        {
            if (method == null)
            {
                throw new ArgumentNullException(nameof(method));
            }
            string name = method.Name;

            if (method.ContainsGenericParameters)
            {
                name += '<';
                name += String.Join(",", method.GetGenericArguments().Select(a => a.Name));
                name += '>';
            }
            if (protection != AccessProtectionOption.None)
            {
                @this.AppendAccessProtection(method, protection);
            }
            @this.Append(frontModifier)
            .AppendCSharpName(method.ReturnType, true, true, useValueTupleParentheses: true)
            .Space()
            .Append(name)
            .AppendParameters(method.GetParameters());
            return(@this);
        }
 static void OpenSwitchOnNameBlock(ICodeWriter read)
 {
     read.Append("if( r.TokenType != System.Text.Json.JsonTokenType.StartArray ) throw new System.Text.Json.JsonException( \"Expecting Json Type array.\" );").NewLine()
     .Append("r.Read();").NewLine()
     .Append("string name = r.GetString();").NewLine()
     .Append("r.Read();").NewLine()
     .Append("switch( name )")
     .OpenBlock();
 }
 public override void GenerateRead(ICodeWriter read, string variableName, bool assignOnly)
 {
     if (!TypeInfo.IsFinal)
     {
         read.Append(variableName).Append(" = (").Append(GenCSharpName).Append(")PocoDirectory_CK.ReadObject( ref r, options, true );").NewLine();
     }
     else
     {
         DoGenerateRead(read, variableName, assignOnly);
     }
 }
 public override void GenerateWrite(ICodeWriter write, string variableName, bool?withType = null)
 {
     if (TypeInfo == JsonTypeInfo.ObjectType || (withType == null && !TypeInfo.IsFinal))
     {
         write.Append("PocoDirectory_CK.WriteObject( w, ").Append(variableName).Append(", options, true );").NewLine();
     }
     else
     {
         this.DoGenerateWrite(write, variableName, handleNull: true, withType ?? false);
     }
 }
 public override void GenerateRead(ICodeWriter read, string variableName, bool assignOnly)
 {
     if (_mapping.TypeInfo == JsonTypeInfo.ObjectType)
     {
         read.Append(variableName).Append(" = (").Append(GenCSharpName).Append(")PocoDirectory_CK.ReadObject( ref r, options, false );").NewLine();
     }
     else
     {
         _mapping.GenerateRead(read, variableName, assignOnly);
     }
 }
Exemple #7
0
        internal static ICodeWriter AddParameter(this ICodeWriter @this, ParameterInfo p)
        {
            if (p.IsIn)
            {
                @this.Append("in ");
            }
            else if (p.IsOut)
            {
                @this.Append("out ");
            }
            else if (p.ParameterType.IsByRef)
            {
                @this.Append("ref ");
            }
            Type?parameterType = p.ParameterType.IsByRef ? p.ParameterType.GetElementType() : p.ParameterType;

            return(@this.AppendCSharpName(parameterType, true, true, useValueTupleParentheses: true)
                   .Space()
                   .AppendVariable(p.Name !));
        }
Exemple #8
0
        internal static ICodeWriter AppendParameters(this ICodeWriter @this, IReadOnlyList <ParameterInfo> parameters)
        {
            if (parameters.Count == 0)
            {
                return(@this.Append("()"));
            }
            @this.Append("( ");
            bool isFirstParameter = true;

            foreach (var p in parameters)
            {
                if (isFirstParameter)
                {
                    isFirstParameter = false;
                }
                else
                {
                    @this.Append(", ");
                }
                @this.AddParameter(p);
            }
            return(@this.Append(" )"));
        }
Exemple #9
0
 internal static ICodeWriter AppendAccessProtection(this ICodeWriter w, MethodInfo method, AccessProtectionOption p)
 {
     Debug.Assert(p != AccessProtectionOption.None);
     if (p == AccessProtectionOption.ThrowOnPureInternal &&
         (method.IsAssembly || method.IsFamilyAndAssembly))
     {
         throw new ArgumentException($"Method {method} must not be internal.", nameof(method));
     }
     if (method.IsPublic)
     {
         w.Append("public ");
     }
     else if (method.IsFamily)
     {
         w.Append("protected ");
     }
     else if (method.IsAssembly)
     {
         if (p == AccessProtectionOption.All)
         {
             w.Append("internal ");
         }
     }
     else if (method.IsFamilyAndAssembly)
     {
         if (p == AccessProtectionOption.All)
         {
             w.Append("private protected ");
         }
         else
         {
             w.Append("protected ");
         }
     }
     else if (method.IsFamilyOrAssembly)
     {
         if (p == AccessProtectionOption.All)
         {
             w.Append("internal protected ");
         }
         else
         {
             w.Append("protected ");
         }
     }
     return(w);
 }
Exemple #10
0
 public static void GenerateAutoInstantiatedNewAssignation(this IPocoSupportResult @this, ICodeWriter writer, string variableName, Type autoType)
 {
     writer.Append(variableName).Append(" = ");
     if (@this.AllInterfaces.TryGetValue(autoType, out IPocoInterfaceInfo? info))
     {
         writer.Append("new ").Append(info.Root.PocoClass.FullName !).Append("();").NewLine();
         return;
     }
     if (@this.PocoClass.ByType.ContainsKey(autoType))
     {
         writer.Append("new ").AppendCSharpName(autoType, true, true, true).Append("();").NewLine();
         return;
     }
     if (autoType.IsGenericType)
     {
         Type genType = autoType.GetGenericTypeDefinition();
         if (genType == typeof(List <>))
         {
             writer.Append("new List<").AppendCSharpName(autoType.GetGenericArguments()[0], true, true, true).Append(">();").NewLine();
             return;
         }
         if (genType == typeof(Dictionary <,>))
         {
             writer.Append("new Dictionary<")
             .AppendCSharpName(autoType.GetGenericArguments()[0], true, true, true)
             .Append(',')
             .AppendCSharpName(autoType.GetGenericArguments()[1], true, true, true)
             .Append(">();")
             .NewLine();
             return;
         }
         if (genType == typeof(HashSet <>))
         {
             writer.Append("new HashSet<").AppendCSharpName(autoType.GetGenericArguments()[0], true, true, true).Append(">();").NewLine();
             return;
         }
     }
     Throw.ArgumentException($"Invalid type '{autoType.FullName}': readonly properties can only be IPoco (that are not marked with [CKTypeDefiner] or [CKTypeSuperDefiner]), Poco objects (marked with a [PocoClass] attribute), HashSet<>, List<>, or Dictionary<,>.", nameof(autoType));
 }
Exemple #11
0
 static void WriteNumber(ICodeWriter write, string variableName)
 {
     write.Append("w.WriteNumberValue( ").Append(variableName).Append(" );");
 }