示例#1
0
        public static string DetermineType(Type t, ref HashSet <string> namespaces, GrassOptions options)
        {
            namespaces.Add(t.Namespace);

            if (t.IsGenericType)
            {
                var name = t.Name.Split(new[] { '`' })[0];

                var genericParams = new List <string>();

                foreach (var genericArguement in t.GetGenericArguments())
                {
                    genericParams.Add(DetermineType(genericArguement, ref namespaces, options));
                }

                return(string.Format("{0}<{1}>", name, string.Join(", ", genericParams.ToArray())));
            }

            if (t == typeof(void))
            {
                return("void");
            }

            if (t.Name.Equals("object", StringComparison.OrdinalIgnoreCase) && options.UseDynamic)
            {
                return("dynamic");
            }

            return(t.Name.Replace("&", "")); // Fix ref type issues
        }
示例#2
0
        public ClassDefinition(string qualifiedAssemblyName, string targetNamespace, GrassOptions options)
        {
            QualifiedAssemblyName = qualifiedAssemblyName;
            Namespace             = targetNamespace;
            MinimumVisibility     = options.MinimumVisibility;
            IsPartial             = options.GeneratePartialClass;
            GenerateNames();

            RequiredNamespaces = new HashSet <string>();
            RequiredNamespaces.Add(AsType.Namespace);
        }
示例#3
0
        public MethodSignature(MethodInfo info, GrassOptions options) : this()
        {
            Info          = info;
            Accessability = GetMethodVisibility(info);
            ReturnType    = TypeHelper.DetermineType(info.ReturnType, ref _requiredNamespaces, options);
            Name          = info.Name;
            this.Options  = options;

            foreach (var p in info.GetParameters())
            {
                Parameters.Add(new ParameterSignature(p, options));
            }
        }
示例#4
0
        public CodeTypeDeclaration GenerateCodeType(string name, GrassOptions options)
        {
            CodeTypeDeclaration targetInterface = new CodeTypeDeclaration(name);

            if (options.MinimumVisibility.HasFlag(Visibility.Public))
            {
                targetInterface.TypeAttributes = TypeAttributes.Public;
            }
            else
            {
                targetInterface.TypeAttributes = TypeAttributes.NestedAssembly | TypeAttributes.NotPublic;
            }

            targetInterface.CustomAttributes.Add(new CodeAttributeDeclaration("GeneratedCode", new CodeAttributeArgument(new CodePrimitiveExpression(Assembly.GetAssembly(typeof(Grass)).GetName().Version.ToString())), new CodeAttributeArgument(new CodePrimitiveExpression("ArtisanCode.Grass"))));
            return(targetInterface);
        }
示例#5
0
        public void PopulateStaticMethods(GrassOptions options)
        {
            Methods = new List <MethodSignature>();

            var type = Type.GetType(QualifiedAssemblyName);

            var accessor = BindingFlags.Public;

            if (EnumHelper.HasFlag(MinimumVisibility, Visibility.Internal) ||
                EnumHelper.HasFlag(MinimumVisibility, Visibility.Protected) ||
                EnumHelper.HasFlag(MinimumVisibility, Visibility.Private))
            {
                accessor |= BindingFlags.NonPublic;
            }

            var methods = type.GetMethods(accessor | BindingFlags.Static);

            foreach (var info in methods)
            {
                Methods.Add(new MethodSignature(info, options));
            }
        }
示例#6
0
 public ParameterSignature(ParameterInfo info, GrassOptions options) : this()
 {
     Info = info;
     Name = info.Name;
     Type = TypeHelper.DetermineType(info.ParameterType, ref _requiredNamespaces, options);
 }
示例#7
0
        public void EmitInterfaceMethods(ref CodeTypeDeclaration targetInterface, ClassDefinition staticClass, GrassOptions options)
        {
            foreach (var m in staticClass.Methods.Where(x => x.Accessability >= options.MinimumVisibility).OrderBy(x => x.Name))
            {
                var method = EmitFunctionSignature(staticClass, m);

                targetInterface.Members.Add(method);
            }
        }
示例#8
0
        public CodeGenerationResult EmitStaticWrapperClass(string targetNamespace, ClassDefinition staticClass, GrassOptions options)
        {
            CodeCompileUnit targetUnit       = new CodeCompileUnit();
            CodeNamespace   emittedNamespace = GenerateEmittedNamespace(targetNamespace, staticClass);

            string className      = string.Format("{0}Wrapper", staticClass.ClassName);
            string outputFileName = string.Format("{0}.{1}", className, FileExtension);
            CodeTypeDeclaration targetStaticWrapper = GenerateCodeType(className, options);

            targetStaticWrapper.IsClass   = true;
            targetStaticWrapper.IsPartial = options.GeneratePartialClass;

            var interfaceRef = new CodeTypeReference(new CodeTypeParameter(staticClass.InterfaceName));

            // Inherit from Object (Required for the VB CodeDom to generate the Implements keyword)
            targetStaticWrapper.BaseTypes.Add(new CodeTypeReference(typeof(System.Object)));
            targetStaticWrapper.BaseTypes.Add(interfaceRef);

            EmitStaticWrapperClassMethods(ref targetStaticWrapper, staticClass, options);

            emittedNamespace.Types.Add(targetStaticWrapper);

            targetUnit.Namespaces.Add(emittedNamespace);

            return(new CodeGenerationResult(outputFileName, targetUnit));
        }
示例#9
0
        public CodeGenerationResult EmitInterface(string targetNamespace, ClassDefinition staticClass, GrassOptions options)
        {
            CodeCompileUnit targetUnit       = new CodeCompileUnit();
            CodeNamespace   emittedNamespace = GenerateEmittedNamespace(targetNamespace, staticClass);

            string outputFileName = string.Format("{0}.{1}", staticClass.InterfaceName, FileExtension);
            CodeTypeDeclaration targetInterface = GenerateCodeType(staticClass.InterfaceName, options);

            targetInterface.IsInterface = true;
            EmitInterfaceMethods(ref targetInterface, staticClass, options);

            emittedNamespace.Types.Add(targetInterface);

            targetUnit.Namespaces.Add(emittedNamespace);

            return(new CodeGenerationResult(outputFileName, targetUnit));
        }
示例#10
0
        public void EmitStaticWrapperClassMethods(ref CodeTypeDeclaration targetStaticClass, ClassDefinition staticClass, GrassOptions options)
        {
            foreach (var m in staticClass.Methods.Where(x => x.Accessability >= options.MinimumVisibility).OrderBy(x => x.Name))
            {
                CodeMemberMethod method = EmitFunctionSignature(staticClass, m);
                method.Attributes = MemberAttributes.Public;
                method.ImplementationTypes.Add(new CodeTypeReference(new CodeTypeParameter(staticClass.InterfaceName)));


                CodeExpression[] methodParameters = m.Parameters.Select(x => GenParameterExpression(x)).ToArray();

                var callStaticMethodExpr = new CodeMethodInvokeExpression(
                    new CodeTypeReferenceExpression(staticClass.ClassName), m.Name, methodParameters);

                if (m.ReturnType != "void")
                {
                    method.Statements.Add(new CodeMethodReturnStatement(callStaticMethodExpr));
                }
                else
                {
                    method.Statements.Add(callStaticMethodExpr);
                }

                targetStaticClass.Members.Add(method);
            }
        }
示例#11
0
        private void DetermineTypeTest <T>(string expectedOutput, string expectedNamespaceAdded, GrassOptions testOptions = null)
        {
            var options = testOptions ?? new GrassOptions();

            var namespaces = new HashSet <string>();

            var output = TypeHelper.DetermineType(typeof(T), ref namespaces, options);

            Assert.AreEqual(expectedOutput, output);
            Assert.IsTrue(namespaces.Contains(expectedNamespaceAdded));
        }