예제 #1
0
        public static bool IsReturnTypeNonNull(TypeReference t, Generator gen)
        {
            var nonNullReturnTypes = new string[]
            {
                "Windows.Foundation.IAsyncAction",
                "Windows.Foundation.IAsyncActionWithProgress`1",
                "Windows.Foundation.IAsyncOperation`1",
                "Windows.Foundation.IAsyncOperationWithProgress`2"
            };

            if (nonNullReturnTypes.Contains(t.GetElementType().FullName))
            {
                return(true);
            }

            if (t.IsGenericParameter || t.IsArray || t.IsValueType || t.IsPrimitive ||
                t.FullName == "System.String" || t.FullName == "System.Object" || t.FullName == "System.Guid")
            {
                return(false);
            }
            else
            {
                // we mark class types as non-null if their default interface is non-null
                var def = gen.GetTypeDefinition(t);
                if (def.Kind == TypeKind.Class)
                {
                    var defaultInterface = TypeHelpers.GetDefaultInterface(def.Type);
                    return(nonNullReturnTypes.Contains(defaultInterface.GetElementType().FullName));
                }
                else
                {
                    return(false);
                }
            }
        }
예제 #2
0
        public override void CollectDependencies()
        {
            foreach (var staticType in statics)
            {
                AddDependency(Generator.GetTypeDefinition(staticType));
            }

            factories = GetFactoryTypes().Select(f => TypeHelpers.GetTypeName(Generator, this, f, TypeUsage.Alias)).ToArray();

            if (Type.Interfaces.Count > 0)
            {
                var defaultInterface = TypeHelpers.GetDefaultInterface(Type);
                aliasedType = TypeHelpers.GetTypeName(Generator, this, defaultInterface, TypeUsage.Alias);
            }

            var factoryMethods = GetFactoryTypes().OrderBy(f => f.FullName).SelectMany(f => Generator.GetTypeDefinition(f).Methods).ToArray();
            var staticMethods  = statics.OrderBy(s => s.FullName).SelectMany(s => Generator.GetTypeDefinition(s).Methods).ToArray();

            foreach (var m in factoryMethods)
            {
                methodWrappers.Add(new ClassMethodDef(m, this, true));
            }
            foreach (var m in staticMethods)
            {
                methodWrappers.Add(new ClassMethodDef(m, this, false));
            }

            // fix name clashes in method wrappers (caused by overloads from different interfaces)
            foreach (var nameToFix in methodWrappers.GroupBy(m => m.Name).Where(g => g.Count() > 1))
            {
                // we expect that there is a single base name (modulo suffix numbers) for all clashing interfaces
                string baseName = nameToFix.Select(n => n.WrappedMethod.DeclaringType.Name.TrimEnd('1', '2', '3', '4', '5', '6', '7', '8', '9', '0')).Distinct().Single();
                foreach (var m in nameToFix)
                {
                    // so far this is always "2"
                    var nameSuffix = m.WrappedMethod.DeclaringType.Name.Replace(baseName, "");
                    m.FixupName(nameSuffix);
                }
            }
        }