当前位置: 首页>>代码示例>>C#>>正文


C# Compilation.ClassifyConversion方法代码示例

本文整理汇总了C#中Compilation.ClassifyConversion方法的典型用法代码示例。如果您正苦于以下问题:C# Compilation.ClassifyConversion方法的具体用法?C# Compilation.ClassifyConversion怎么用?C# Compilation.ClassifyConversion使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在Compilation的用法示例。


在下文中一共展示了Compilation.ClassifyConversion方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: FindCommonType

        /// <summary>
        /// Takes a set of types and returns the most "constrained" type that all of them can be converted to.
        /// 
        /// Worst case, it returns a TypeSyntax for `object`.
        /// 
        /// If the appropriate type is an anonymous one, `replaceType` contains a Tuple of that anonymous type and the 
        /// name of a new one that should be created to take its place.
        /// </summary>
        private static TypeSyntax FindCommonType(IEnumerable<TypeInfo> info, Compilation comp, out Tuple<NamedTypeSymbol, string> replaceType)
        {
            if (info.Any(a => a.Equals(TypeInfo.None)))
            {
                replaceType = null;
                return null;
            }

            var allPossibilities = info.SelectMany(i => i.Type.AllInterfaces).OfType<NamedTypeSymbol>().ToList();

            allPossibilities.AddRange(info.Select(s => s.Type).OfType<NamedTypeSymbol>());

            foreach (var i in info)
            {
                var @base = i.Type.BaseType;
                while (@base != null)
                {
                    allPossibilities.Add(@base);
                    @base = @base.BaseType;
                }
            }

            // Order so that subclasses are before superclasses, classes are before interfaces,
            //   special interfaces first, and generic types are before non-generic ones
            //   with the special case that System.Object is always the *last* option
            //   uses type name as a tie-breaker for ordering
            //
            // Special interfaces are anything that gets special treatment in the language, which for now is IEnumerable<T> and IEnumerable
            allPossibilities.Sort(
                (a, b) =>
                {
                    var aIsNotObject = a.TypeKind != TypeKind.Class || a.BaseType != null;
                    var bIsNotObject = b.TypeKind != TypeKind.Class || b.BaseType != null;

                    var aIsNotInterface = a.TypeKind != TypeKind.Interface;
                    var bIsNotInferface = b.TypeKind != TypeKind.Interface;

                    // Get both IEnumerable && IEnumerable<T>
                    var aIsSpecial = a.Name == "IEnumerable";
                    var bIsSpecial = b.Name == "IEnumerable";

                    var aIsSubclassOfB = IsSubclass(a, b);
                    var bIsSubclassOfA = IsSubclass(b, a);

                    if (aIsNotObject && !bIsNotObject) return -1;
                    if (bIsNotObject && !aIsNotObject) return 1;

                    if (aIsSubclassOfB) return -1;
                    if (bIsSubclassOfA) return 1;

                    if (aIsNotInterface && !bIsNotInferface) return -1;
                    if (bIsNotInferface && !aIsNotInterface) return 1;

                    if (!aIsNotInterface && !bIsNotInferface)
                    {
                        if (aIsSpecial && !bIsSpecial) return -1;
                        if (bIsSpecial && !aIsSpecial) return 1;
                    }

                    if (a.IsGenericType && !b.IsGenericType) return -1;
                    if (b.IsGenericType && !a.IsGenericType) return 1;

                    return a.MetadataName.CompareTo(b.MetadataName);
                }
            );

            var commonType =
                allPossibilities.First(
                    t =>
                    {
                        var conversions = info.Select(a => comp.ClassifyConversion(a.Type, t)).ToList();

                        return conversions.All(a => a.Exists && a.IsImplicit);
                    }
                );

            string fullName;

            // we actually need to *replace* this type so we can put it in the returns
            if (commonType.IsAnonymousType)
            {
                fullName = "__FTI" + Guid.NewGuid().ToString().Replace("-", "");
                replaceType = Tuple.Create(commonType, fullName);
            }
            else
            {
                fullName = commonType.ToDisplayString(SymbolDisplayFormat.FullyQualifiedFormat);
                replaceType = null;
            }

            return Syntax.ParseTypeName(fullName);
        }
开发者ID:kevin-montrose,项目名称:further-type-inferencing,代码行数:100,代码来源:Program.cs

示例2: DoesConversionExist

 protected override bool DoesConversionExist(Compilation compilation, ITypeSymbol source, ITypeSymbol destination)
 {
     return compilation.ClassifyConversion(source, destination).Exists;
 }
开发者ID:RoryVL,项目名称:roslyn,代码行数:4,代码来源:CSharpAddAsyncCodeFixProvider.cs

示例3: AreEquivalent

        internal static bool AreEquivalent(TypeSymbol a, TypeSymbol b, Compilation comp)
        {
            var aMembers = a.GetMembers().OfType<PropertySymbol>().ToList();
            var bMembers = b.GetMembers().OfType<PropertySymbol>().ToList();

            if (aMembers.Count != bMembers.Count) return false;

            for (var i = 0; i < aMembers.Count; i++)
            {
                var aMember = aMembers[i];
                var bMember = bMembers[i];

                if (aMember.Name != bMember.Name) return false;
                if (aMember.DeclaredAccessibility != bMember.DeclaredAccessibility) return false;

                var aType = aMember.Type;
                var bType = bMember.Type;

                var aName = aType.ToDisplayString(SymbolDisplayFormat.FullyQualifiedFormat);
                var bName = bType.ToDisplayString(SymbolDisplayFormat.FullyQualifiedFormat);

                if (aName == bName) continue;

                var conv = comp.ClassifyConversion(aType, bType);

                if (!conv.IsIdentity) return false;
            }

            return true;
        }
开发者ID:kevin-montrose,项目名称:further-type-inferencing,代码行数:30,代码来源:Visitors.cs


注:本文中的Compilation.ClassifyConversion方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。