示例#1
0
        // TODO -- need ut's
        public GeneratedType InheritsFrom(Type baseType)
        {
            var ctors = baseType.GetConstructors();

            if (ctors.Length > 1)
            {
                throw new ArgumentOutOfRangeException(nameof(baseType),
                                                      $"The base type for the code generation must only have one public constructor. {baseType.FullNameInCode()} has {ctors.Length}");
            }

            if (ctors.Length == 1)
            {
                var baseArguments = ctors.Single().GetParameters()
                                    .Select(x => new InjectedField(x.ParameterType, x.Name)).ToArray();

                BaseConstructorArguments = baseArguments.Select(x => x.ToBaseCtorVariable()).ToArray();

                AllInjectedFields.AddRange(baseArguments);
            }


            BaseType = baseType;
            foreach (var methodInfo in baseType.GetMethods().Where(x => x.DeclaringType != typeof(object))
                     .Where(x => x.CanBeOverridden()))
            {
                _methods.Add(new GeneratedMethod(methodInfo)
                {
                    Overrides = true
                });
            }


            return(this);
        }
示例#2
0
        public void UseConstantForBaseCtor(Variable variable)
        {
            var index = BaseConstructorArguments.GetFirstIndex(v => v.VariableType == variable.VariableType);

            if (index < 0)
            {
                throw new InvalidOperationException("No base constructor arguments of type " + variable.VariableType.FullNameInCode());
            }

            BaseConstructorArguments[index] = variable;
            AllInjectedFields.RemoveAt(index);
        }
示例#3
0
        public void Write(ISourceWriter writer)
        {
            writeDeclaration(writer);

            if (AllInjectedFields.Any())
            {
                writeFieldDeclarations(writer, AllInjectedFields);
                writeConstructorMethod(writer, AllInjectedFields);
            }

            writeSetters(writer);


            foreach (var method in _methods.Where(x => x.WillGenerate()))
            {
                writer.BlankLine();
                method.WriteMethod(writer);
            }

            writer.FinishBlock();
        }
示例#4
0
 Variable IVariableSource.Create(Type type)
 {
     return(AllInjectedFields.FirstOrDefault(x => x.ArgType == type));
 }
示例#5
0
 bool IVariableSource.Matches(Type type)
 {
     return(AllInjectedFields.Any(x => x.VariableType == type));
 }