예제 #1
0
        public void TestCase_OnlyDirectlyImplemented()
        {
            var type   = GetTypeDef <TestClassWithInterfaces> ();
            var ifaces = DocUtils.GetUserImplementedInterfaces(type).ToArray();

            Assert.AreEqual(2, ifaces.Count());
            Assert.AreEqual("I2", ifaces[0].Name);
            Assert.AreEqual("ICombined", ifaces[1].Name);
        }
        protected override string GetTypeDeclaration(TypeDefinition type)
        {
            StringBuilder buf = new StringBuilder();

            var genericParamList = GetTypeSpecifiGenericParameters(type);

            AppendGenericItem(buf, genericParamList);
            AppendGenericTypeConstraints(buf, type);

            AppendWebHostHiddenAttribute(buf, type);

            buf.Append(GetTypeKind(type));
            buf.Append(" ");
            var cppType = GetCppType(type.FullName);

            buf.Append(cppType == null
                    ? GetNameWithOptions(type, false, false)
                    : cppType);

            if (type.IsSealed && !DocUtils.IsDelegate(type) && !type.IsValueType)
            {
                buf.Append(" final");
            }

            CppWinRtFullMemberFormatter full = new CppWinRtFullMemberFormatter(this.TypeMap);

            if (!type.IsEnum)
            {
                TypeReference basetype = type.BaseType;
                if (basetype != null && basetype.FullName == "System.Object" || type.IsValueType)   // FIXME
                {
                    basetype = null;
                }

                List <string> interfaceNames;
                try
                {
                    //for winRt Resolve() can fail as Cecil understands winRt types as .net (eg, "System.Object" cannot be resolved)
                    interfaceNames = DocUtils.GetUserImplementedInterfaces(type)
                                     .Select(iface => full.GetNameWithOptions(iface, true, false))
                                     .OrderBy(s => s)
                                     .ToList();
                }
                catch
                {
                    interfaceNames = null;
                }

                if (basetype != null || interfaceNames?.Count > 0)
                {
                    buf.Append(" : ");
                }

                if (basetype != null)
                {
                    buf.Append(full.GetNameWithOptions(basetype, true, false));
                    if (interfaceNames?.Count > 0)
                    {
                        buf.Append(", ");
                    }
                }

                for (int i = 0; i < interfaceNames?.Count; i++)
                {
                    if (i != 0)
                    {
                        buf.Append(", ");
                    }
                    buf.Append(interfaceNames?[i]);
                }
            }

            return(buf.ToString());
        }
        protected bool HasUnsupportedParent(TypeDefinition typedef)
        {
            var            collect            = new List <TypeDefinition>();
            TypeReference  baseTypeReference  = typedef.BaseType;
            TypeDefinition basetypeDefenition = null;

            var allowedTypes = GetAllowedTypes();

            try
            {
                //iterate through all classes in in inheritance hierarhy to exclude usage of .net types
                basetypeDefenition = baseTypeReference?.Resolve();
                while (basetypeDefenition != null)
                {
                    if (allowedTypes.Contains(basetypeDefenition.FullName) || basetypeDefenition.BaseType == null)
                    {
                        break;
                    }

                    collect.Add(basetypeDefenition);

                    //needs to call Resolve to grab all base classes
                    basetypeDefenition = basetypeDefenition.BaseType?.Resolve();
                }
            }
            catch (Exception)
            {
                //for c++/cx Resolve() can fail as Cecil understands types as .net (eg, "System.Attribute" instead of "Platform::Metadata::Attribute")
                //needs to ignore those errors
                baseTypeReference = basetypeDefenition?.BaseType ?? baseTypeReference;
            }

            if (collect.Any(x => DocUtils.GetNamespace(x).StartsWith("System.") || DocUtils.GetNamespace(x).Equals("System")) ||
                !allowedTypes.Contains(baseTypeReference?.FullName) &&
                (
                    DocUtils.GetNamespace(baseTypeReference).StartsWith("System.") ||
                    DocUtils.GetNamespace(baseTypeReference).Equals("System")
                )
                )
            {
                //can only be Windows Runtime types(no support for .net types)
                return(true);
            }

            try
            {
                IEnumerable <TypeReference> interfaceNames = DocUtils.GetUserImplementedInterfaces(typedef).ToList();

                if (interfaceNames.Any(x => DocUtils.GetNamespace(x).StartsWith("System.") || DocUtils.GetNamespace(x).Equals("System")))
                {
                    //can only be Windows Runtime types(no support for .net types)
                    return(true);
                }
            }
            catch
            {
                //for c++/cx Resolve() can fail as Cecil understands types as .net (eg, "System.Attribute" instead of "Platform::Metadata::Attribute")
                //needs to ignore those errors
            }

            return(false);
        }
        protected override string GetTypeDeclaration(TypeDefinition type)
        {
            string visibility = GetTypeVisibility(type.Attributes);

            if (visibility == null)
            {
                return(null);
            }

            StringBuilder buf = new StringBuilder();

            if (!visibility.Contains(":"))
            {
                AppendWebHostHiddenAttribute(buf, type);
            }

            buf.Append(visibility);
            buf.Append(" ");

            if (visibility.Contains(":"))
            {
                AppendWebHostHiddenAttribute(buf, type);
            }

            CppFullMemberFormatter full = new CppCxFullMemberFormatter(this.TypeMap);

            if (DocUtils.IsDelegate(type))
            {
                buf.Append("delegate ");
                MethodDefinition invoke = type.GetMethod("Invoke");
                buf.Append(full.GetNameWithOptions(invoke.ReturnType)).Append(" ");
                buf.Append(GetNameWithOptions(type, false, false));
                AppendParameters(buf, invoke, invoke.Parameters);
                buf.Append(";");

                return(buf.ToString());
            }

            buf.Append(GetTypeKind(type));
            buf.Append(" ");
            buf.Append(GetCppType(type.FullName) == null
                    ? GetNameWithOptions(type, false, false)
                    : type.Name);

            if (type.IsAbstract && !type.IsInterface)
            {
                buf.Append(" abstract");
            }
            if (type.IsSealed && !DocUtils.IsDelegate(type) && !type.IsValueType)
            {
                buf.Append(" sealed");
            }

            if (!type.IsEnum)
            {
                TypeReference basetype = type.BaseType;
                if (basetype != null && basetype.FullName == "System.Object" || type.IsValueType)   // FIXME
                {
                    basetype = null;
                }

                List <string> interfaceNames;
                try
                {
                    //for c++/cx Resolve() can fail as Cecil understands CX types as .net (eg, "System.Attribute" instead of "Platform::Metadata::Attribute")
                    interfaceNames = DocUtils.GetUserImplementedInterfaces(type)
                                     .Select(iface => full.GetNameWithOptions(iface, true, false))
                                     .OrderBy(s => s)
                                     .ToList();
                }
                catch
                {
                    interfaceNames = null;
                }

                if (basetype != null || interfaceNames?.Count > 0)
                {
                    buf.Append(" : ");
                }

                if (basetype != null)
                {
                    var appendValue = GetCppType(basetype.FullName);
                    buf.Append(appendValue ?? full.GetNameWithOptions(basetype, true, false));
                    if (interfaceNames?.Count > 0)
                    {
                        buf.Append(", ");
                    }
                }

                for (int i = 0; i < interfaceNames?.Count; i++)
                {
                    if (i != 0)
                    {
                        buf.Append(", ");
                    }
                    buf.Append(interfaceNames?[i]);
                }
            }

            return(buf.ToString());
        }
        protected override string GetTypeDeclaration(TypeDefinition type)
        {
            string visibility = GetTypeVisibility(type.Attributes);

            if (visibility == null)
            {
                return(null);
            }

            StringBuilder buf = new StringBuilder();

            var genericParamList = GetTypeSpecifiGenericParameters(type);

            if (!visibility.Contains(":"))
            {
                AppendGenericItem(buf, genericParamList);
                AppendGenericTypeConstraints(buf, type);
            }

            buf.Append(visibility);
            buf.Append(" ");

            if (visibility.Contains(":"))
            {
                AppendGenericItem(buf, genericParamList);
                AppendGenericTypeConstraints(buf, type);
            }

            CppFullMemberFormatter full = new CppFullMemberFormatter(this.TypeMap);

            if (DocUtils.IsDelegate(type))
            {
                buf.Append("delegate ");
                MethodDefinition invoke = type.GetMethod("Invoke");
                buf.Append(full.GetNameWithOptions(invoke.ReturnType));
                buf.Append(" ");
                buf.Append(GetNameWithOptions(type, false, false));
                AppendParameters(buf, invoke, invoke.Parameters);
                buf.Append(";");

                return(buf.ToString());
            }

            buf.Append(GetTypeKind(type));
            buf.Append(" ");
            buf.Append(GetCppType(type.FullName) == null
                    ? GetNameWithOptions(type, false, false)
                    : type.Name);

            if (type.IsAbstract && !type.IsInterface)
            {
                buf.Append(" abstract");
            }
            if (type.IsSealed && !DocUtils.IsDelegate(type) && !type.IsValueType)
            {
                buf.Append(" sealed");
            }

            if (!type.IsEnum)
            {
                TypeReference basetype = type.BaseType;
                if (basetype != null && basetype.FullName == "System.Object" || type.IsValueType)   // FIXME
                {
                    basetype = null;
                }

                List <string> interfaceNames = DocUtils.GetUserImplementedInterfaces(type)
                                               .Select(iface => full.GetNameWithOptions(iface, true, false))
                                               .OrderBy(s => s)
                                               .ToList();

                if (basetype != null || interfaceNames.Count > 0)
                {
                    buf.Append(" : ");
                }

                if (basetype != null)
                {
                    buf.Append(full.GetNameWithOptions(basetype, true, false));
                    if (interfaceNames.Count > 0)
                    {
                        buf.Append(", ");
                    }
                }

                for (int i = 0; i < interfaceNames.Count; i++)
                {
                    if (i != 0)
                    {
                        buf.Append(", ");
                    }
                    buf.Append(interfaceNames[i]);
                }
            }

            return(buf.ToString());
        }
예제 #6
0
        protected override string GetTypeDeclaration(TypeDefinition type)
        {
            string visibility = GetTypeVisibility(type.Attributes);

            if (visibility == null)
            {
                return(null);
            }

            StringBuilder buf = new StringBuilder();

            buf.Append(visibility);
            buf.Append(" ");

            MemberFormatter full = new CSharpFullMemberFormatter(this.TypeMap);

            if (DocUtils.IsDelegate(type))
            {
                buf.Append("delegate ");
                MethodDefinition invoke = type.GetMethod("Invoke");
                var context             = AttributeParserContext.Create(invoke.MethodReturnType);
                var isNullableType      = context.IsNullable();
                buf.Append(full.GetName(invoke.ReturnType, context));
                buf.Append(GetTypeNullableSymbol(invoke.ReturnType, isNullableType));
                buf.Append(" ");
                buf.Append(GetName(type));
                AppendParameters(buf, invoke, invoke.Parameters);
                AppendGenericTypeConstraints(buf, type);
                buf.Append(";");

                return(buf.ToString());
            }

            if (type.IsAbstract && !type.IsInterface)
            {
                buf.Append("abstract ");
            }
            if (type.IsSealed && !DocUtils.IsDelegate(type) && !type.IsValueType)
            {
                buf.Append("sealed ");
            }
            buf.Replace("abstract sealed", "static");

            buf.Append(GetTypeKind(type));
            buf.Append(" ");
            buf.Append(GetCSharpType(type.FullName) == null
                    ? GetName(type)
                    : type.Name);

            if (!type.IsEnum)
            {
                TypeReference basetype = type.BaseType;
                if (basetype != null && basetype.FullName == "System.Object" || type.IsValueType)   // FIXME
                {
                    basetype = null;
                }

                List <string> interface_names = DocUtils.GetUserImplementedInterfaces(type)
                                                .Select(iface => full.GetName(iface))
                                                .OrderBy(s => s)
                                                .ToList();

                if (basetype != null || interface_names.Count > 0)
                {
                    buf.Append(" : ");
                }

                if (basetype != null)
                {
                    buf.Append(full.GetName(basetype));
                    if (interface_names.Count > 0)
                    {
                        buf.Append(", ");
                    }
                }

                for (int i = 0; i < interface_names.Count; i++)
                {
                    if (i != 0)
                    {
                        buf.Append(", ");
                    }
                    buf.Append(interface_names[i]);
                }
                AppendGenericTypeConstraints(buf, type);
            }

            return(buf.ToString());
        }