Beispiel #1
0
        /// <summary>
        /// Generates the method call information declarition.
        /// </summary>
        /// <param name="builder">The builder.</param>
        /// <param name="methodInfo">The method information.</param>
        /// <param name="indent">The indent.</param>
        /// <param name="methodCallInfoVariableName">Name of the method call information variable.</param>
        protected void GenerateMethodCallInfoDeclarition(StringBuilder builder, MethodInfo methodInfo, int indent, string methodCallInfoVariableName)
        {
            if (builder != null && methodInfo != null && !string.IsNullOrWhiteSpace(methodCallInfoVariableName))
            {
                var tmpMethodCallInfo = new MethodCallInfo();
                tmpMethodCallInfo.Fill(methodInfo);

                builder.AppendIndent(indent);
                builder.Append(string.Format("{0} {1} = new {0}(", typeof(MethodCallInfo).ToCodeLook(), methodCallInfoVariableName));
                builder.Append(string.Format("\"{0}\",", tmpMethodCallInfo.MethodFullName));
                builder.Append(" new Dictionary<string, object>{");
                foreach (var one in tmpMethodCallInfo.InArgs)
                {
                    builder.Append(" {\"");
                    builder.Append(one.Key);
                    builder.Append("\", ");
                    builder.Append(one.Key);
                    builder.Append("},");
                }
                builder.RemoveLastIfMatch(StringConstants.CommaChar);
                builder.Append("}");
                builder.AppendLine(")");

                builder.AppendBeginBrace(ref indent);

                builder.AppendIndent(indent);
                builder.Append("SerializableArgNames = new System.Collections.Generic.List<System.String> {");
                builder.Append(CSharpCodeGenerateUtil.CombineCode(tmpMethodCallInfo.SerializableArgNames, (x) => { return(x.AsQuotedString()); }, 16));
                builder.AppendLine("}");

                builder.AppendEndBrace(ref indent);
                builder.AppendLine(";");
            }
        }
        /// <summary>
        /// Creates the deep equality class code.
        /// </summary>
        /// <param name="type">The type.</param>
        /// <param name="typeName">Name of the type.</param>
        /// <param name="stringComparison">The string comparison.</param>
        /// <returns></returns>
        private static string CreateDeepEqualityClassCode(Type type, string typeName, StringComparison stringComparison)
        {
            if (type != null)
            {
                var getProperties = type.GetProperties(System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.GetProperty);
                var getFields     = type.GetFields(System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.GetField);

                StringBuilder builder = new StringBuilder(4096 + getProperties.Length * 256 + getFields.Length * 256);

                var deepEqualityType = typeof(IDeepEquality <>);

                builder.AppendLineWithFormat("public class {0}: {1}", typeName, deepEqualityType.MakeGenericType(type).ToCodeLook());
                builder.AppendBeginBrace();

                // Constructor
                builder.AppendLineWithFormat("public {0}()", typeName);

                builder.AppendBeginBrace();
                builder.AppendEndBrace();

                // DeepEquals
                builder.AppendLineWithFormat("public bool DeepEquals({0} obj1, {0} obj2, StringComparison stringComparison)", type.ToCodeLook());
                builder.AppendBeginBrace();
                builder.AppendLine("if(obj1 != null && obj2 != null)");
                builder.AppendBeginBrace();

                builder.Append("return");

                StringBuilder booleanTerms = new StringBuilder(getProperties.Length * 256 + getFields.Length * 256);

                FillAndTerms(booleanTerms, getProperties, x => x.PropertyType);

                if (booleanTerms.Length > 0 && getFields.HasItem())
                {
                    booleanTerms.Append(" &&");
                    FillAndTerms(booleanTerms, getFields, x => x.FieldType);
                }
                builder.Append(booleanTerms);
                builder.AppendLine(";");

                builder.AppendEndBrace();
                builder.AppendLine("else");
                builder.AppendBeginBrace();
                builder.AppendLine("return false;");
                builder.AppendEndBrace();
                builder.AppendEndBrace();

                // End of DeepEquals
                CSharpCodeGenerateUtil.AppendEndBrace(builder);

                return(builder.ToString());
            }

            return(string.Empty);
        }
        /// <summary>
        /// Generates the class declaration part.
        /// </summary>
        /// <param name="className">Name of the class.</param>
        /// <param name="interfaces">The interfaces.</param>
        /// <returns></returns>
        protected virtual string GenerateClassDeclarationPart(string className, ICollection <Type> interfaces)
        {
            StringBuilder builder = new StringBuilder("public class ", 512);
            SequencedKeyDictionary <string, List <Type> > genericTypes = new SequencedKeyDictionary <string, List <Type> >();

            interfaces = interfaces ?? new Collection <Type>();

            foreach (var one in interfaces)
            {
                var genericParameterConstraints = GetGenericTypeConstraints(one);
                if (genericParameterConstraints.HasItem())
                {
                    foreach (var g in genericParameterConstraints)
                    {
                        genericTypes.Merge(g.Key, g.Value.ToList());
                    }
                }
            }

            builder.Append(className);

            if (genericTypes.HasItem())
            {
                builder.Append("<");
                builder.Append(CSharpCodeGenerateUtil.CombineCode(genericTypes, x => x.Key, 16, StringConstants.CommaChar));
                builder.Append(">");
            }

            if (BaseClassType != null)
            {
                builder.Append(": ");
                builder.Append(BaseClassType.ToCodeLook());
            }

            builder.AppendLine();

            if (interfaces.HasItem())
            {
                builder.Append(",");
                builder.AppendLine(CSharpCodeGenerateUtil.CombineCode(interfaces, x => x.ToCodeLook(), 32, StringConstants.CommaChar));
                builder.AppendLine();
            }

            // Add generic constraints
            foreach (var constraint in genericTypes)
            {
                CSharpCodeGenerateUtil.InternalWriteGenericConstraintsCodeLook(builder, constraint.Key, constraint.Value, new string(CodeIndent[0], CodeIndent.Length * 3));
                builder.AppendLine();
            }

            return(builder.ToString());
        }
 /// <summary>
 /// Writes the file information.
 /// </summary>
 /// <param name="builder">The builder.</param>
 protected virtual void WriteFileInfo(StringBuilder builder)
 {
     CSharpCodeGenerateUtil.WriteProgrammingIntelligenceFileDescription(builder);
 }