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


C# Type.GetGenericParameterConstraints方法代码示例

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


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

示例1: IsAssemblyQualifiedNameAssignableFrom

 internal static bool IsAssemblyQualifiedNameAssignableFrom(Type t1, Type t2)
 {
     if ((t1 == null) || (t2 == null))
     {
         return false;
     }
     if (!AssemblyQualifiedNameEquals(t1, t2))
     {
         if (IsLooseSubClassOf(t2, t1))
         {
             return true;
         }
         if (t1.IsInterface)
         {
             return LooselyImplementInterface(t2, t1);
         }
         if (!t1.IsGenericParameter)
         {
             return false;
         }
         Type[] genericParameterConstraints = t1.GetGenericParameterConstraints();
         for (int i = 0; i < genericParameterConstraints.Length; i++)
         {
             if (!IsAssemblyQualifiedNameAssignableFrom(genericParameterConstraints[i], t2))
             {
                 return false;
             }
         }
     }
     return true;
 }
开发者ID:pritesh-mandowara-sp,项目名称:DecompliedDotNetLibraries,代码行数:31,代码来源:LooseTypeExtensions.cs

示例2: RecursionSafeReflect

        private IAstTypeReference RecursionSafeReflect(Type type, IDictionary<Type, IAstTypeReference> alreadyReflected)
        {
            var reflected = alreadyReflected.GetValueOrDefault(type);
            if (reflected != null)
                return reflected;

            if (type == typeof(object))
                return AstAnyType.Instance;

            if (type.IsGenericParameter) {
                var constraints = type.GetGenericParameterConstraints();
                return new AstGenericPlaceholderType(
                    type.Name,
                    p => {
                        alreadyReflected.Add(type, p);
                        return constraints.Select(c => this.RecursionSafeReflect(c, alreadyReflected));
                    },
                    target: type
                );
            }

            if (IsFunctionType(type))
                return ReflectFunctionType(type, alreadyReflected);

            if (type.IsGenericType && !type.IsGenericTypeDefinition)
                return ReflectGenericType(type, alreadyReflected);

            return new AstReflectedType(type, this);
        }
开发者ID:ashmind,项目名称:light,代码行数:29,代码来源:Reflector.cs

示例3: GetConstraints

		public static GenericConstraints GetConstraints (Type t)
		{
			Type [] constraints = t.GetGenericParameterConstraints ();
			GenericParameterAttributes attrs = t.GenericParameterAttributes;
			if (constraints.Length == 0 && attrs == GenericParameterAttributes.None)
				return null;
			return new ReflectionConstraints (t.Name, constraints, attrs);
		}
开发者ID:lewurm,项目名称:benchmarker,代码行数:8,代码来源:support.cs

示例4: CheckGenericTypeConstraints

		private static bool CheckGenericTypeConstraints(Type genType, Type parameterType) {
			Type[] constraints = genType.GetGenericParameterConstraints();

			for (int i = 0; i < constraints.Length; i++)
				if (!constraints[i].IsAssignableFrom(parameterType))
					return false;

			return true;
		}
开发者ID:GodLesZ,项目名称:svn-dump,代码行数:9,代码来源:GenericBinder.cs

示例5: CheckConstraints

        public static bool CheckConstraints(Type goal, Type probe)
        {
            var constraints = goal.GetGenericParameterConstraints();

            for (var i = 0; i < constraints.Length; i++)
                if (!constraints[i].IsAssignableFrom(probe))
                    return false;

            return true;
        }
开发者ID:yallie,项目名称:zyan,代码行数:10,代码来源:TypeHelper.cs

示例6: MeetsGenericParameterConstraints

        public static bool MeetsGenericParameterConstraints(this Type type, Type genericArgument)
        {
            if (!genericArgument.IsGenericParameter)
                throw new ArgumentException("The argument must be a generic parameter.", "genericArgument");

            Type[] constraints = genericArgument.GetGenericParameterConstraints();
            for (int i = 0; i < constraints.Length; i++)
            {
                if (!constraints[i].IsAssignableFrom(type))
                    return false;
            }

            return true;
        }
开发者ID:phatboyg,项目名称:ServiceBook,代码行数:14,代码来源:GenericTypeExtensions.cs

示例7: AppendType

        static private void AppendType(Type type, StringBuilder sb, bool showGenericConstraints = false)
        {
            if (showGenericConstraints && type.IsGenericParameter)
            {
                if (type.GenericParameterAttributes.HasFlag(GenericParameterAttributes.ReferenceTypeConstraint)) sb.Append("class ");
                if (type.GenericParameterAttributes.HasFlag(GenericParameterAttributes.NotNullableValueTypeConstraint)) sb.Append("valuetype ");
                if (type.GenericParameterAttributes.HasFlag(GenericParameterAttributes.DefaultConstructorConstraint)) sb.Append(".ctor ");

                var genericConstraints = type.GetGenericParameterConstraints();
                if (genericConstraints.Length > 0)
                {
                    sb.Append("(");
                    foreach (var genericConstraint in genericConstraints)
                    {
                        AppendType(genericConstraint, sb);
                        AppendComma(sb);
                    }
                    RemoveTrailingComma(sb);
                    sb.Append(") ");
                }
            }
            sb.Append(type);
        }
开发者ID:elemk0vv,项目名称:roslyn-1,代码行数:23,代码来源:MetadataSignatureHelper.cs

示例8: Step

        private static void Step(Type current)
        {
            if (types.Contains(current) ||
                current.Name.StartsWith("<") ||
                (current.FullName != null && current.FullName.StartsWith("System") && !(current.IsGenericType && current.GetGenericTypeDefinition() == typeof(IEnumerable<>))))
            {
                return;
            }

            types.Add(current);

            if (current.IsInterface)
            {
                foreach (var type in allTypes)
                {
                    if (type.GetInterfaces().Contains(current))
                    {
                        dependencies.Add(new Tuple<Type, Type>(current, type));
                        Step(type);
                    }
                }

                if (current.IsGenericType)
                {
                    foreach (var type in allTypes)
                    {
                        if (type.GetInterfaces().Where(i => i.IsGenericType).Select(i => i.GetGenericTypeDefinition()).Contains(current.GetGenericTypeDefinition()))
                        {
                            dependencies.Add(new Tuple<Type, Type>(current, type));
                            Step(type);
                        }
                    }
                }
            }

            if (current.IsGenericType)
            {
                var genericArguments = current.GetGenericArguments();

                foreach (Type genericArgument in genericArguments)
                {
                    dependencies.Add(new Tuple<Type, Type>(current, genericArgument));

                    Step(genericArgument);
                }
            }

            if (current.IsGenericParameter)
            {
                foreach (var parameter in current.GetGenericParameterConstraints())
                {
                    dependencies.Add(new Tuple<Type, Type>(current, parameter));
                    Step(parameter);

                    foreach (var type in allTypes)
                    {
                        if (type.BaseType == parameter)
                        {
                            dependencies.Add(new Tuple<Type, Type>(current, type));
                            Step(type);
                        }

                        foreach (Type @interface in type.GetInterfaces())
                        {
                            if (@interface == parameter)
                            {
                                dependencies.Add(new Tuple<Type, Type>(current, @interface));
                                Step(@interface);
                            }

                            if (@interface.IsGenericType)
                            {
                                if (@interface.GetGenericTypeDefinition() == parameter)
                                {
                                    dependencies.Add(new Tuple<Type, Type>(current, @interface));
                                    Step(@interface);
                                }
                            }
                        }
                    }
                }
            }

            if (current.Name.EndsWith("Factory"))
            {
                MethodInfo[] methods = current.GetMethods(BindingFlags.Instance | BindingFlags.Public);
                foreach (MethodInfo method in methods)
                {
                    dependencies.Add(new Tuple<Type, Type>(current, method.ReturnType));
                    Step(method.ReturnType);
                }
            }

            var constructors = current.GetConstructors(BindingFlags.Public | BindingFlags.Instance);
            if (constructors.Count() == 1)
            {
                var parameters = constructors.Single().GetParameters();

                foreach (var parameter in parameters)
                {
//.........这里部分代码省略.........
开发者ID:ursenzler,项目名称:DependencyDumper,代码行数:101,代码来源:Program.cs

示例9: typeToSpec

        private static PlTerm typeToSpec(Type type)
        {
            if (type == null) return PLNULL;
            if (type.IsArray && type.HasElementType)
            {
                if (type.GetArrayRank() != 1)
                {
                    return PlC("arrayOf", typeToSpec(type.GetElementType()), ToProlog(type.GetArrayRank()));
                }
                return PlC("arrayOf", typeToSpec(type.GetElementType()));
            }
            if (type.IsGenericParameter)
            {
                Type[] gt = type.GetGenericParameterConstraints();
                return PlC("<" + type.FullName ?? type.Name + ">", ToPlTermVSpecs(gt));
            }
            if (type.IsPointer)
            {
                Type gt = type.GetElementType();
                return PlC("pointer", typeToSpec(gt));
            }
            if (type.IsByRef)
            {
                Type gt = type.GetElementType();
                return PlC("byref", typeToSpec(gt));
            }
            // @todo if false , use IsGenericType
            if (false) if (typeof(Nullable<>).IsAssignableFrom(type))
            {
                Embedded.Error("@todo Not Implemented NULLABLE");
                Type gt = type.GetElementType();
                return PlC("nullable", typeToSpec(gt));
            }

            if (type.IsGenericType )
            {
                Type gt = type.GetGenericTypeDefinition();
                Type[] gtp = type.GetGenericArguments();
                PlTermV vt = ToPlTermVSpecs(gtp);
                string typeName = type.FullName ?? type.Name;
                int gtpLength = gtp.Length;
                int indexOf = typeName.IndexOf("`" + gtpLength);
                if (indexOf > 0)
                {
                    typeName = typeName.Substring(0, indexOf);
                }
                else
                {
                    Embedded.Debug("cant chop arity {0} off string '{1}' ", gtpLength, typeName);
                }
                return PlC(typeName, vt);
            }
            if (type.HasElementType)
            {
                string named = typeToName(type);
                Embedded.Error("@todo Not Implemented " + named);
                Type gt = type.GetElementType();
                if (gt == type) gt = typeof(object);
                return PlC("elementType", PlTerm.PlAtom(named), typeToSpec(gt));
            }
            if (type.IsSpecialName || String.IsNullOrEmpty(type.Name) || String.IsNullOrEmpty(type.FullName) || String.IsNullOrEmpty(type.Namespace))
            {
                string named = typeToName(type);
                Embedded.Error("@todo Not Implemented " + named);
                Type gt = type.UnderlyingSystemType;
                if (gt == type) gt = typeof (object);
                return PlC("static", PlTerm.PlAtom(named), typeToSpec(gt));
            }
            return PlTerm.PlAtom(typeToName(type));
        }
开发者ID:swi-to-yap,项目名称:swicli,代码行数:70,代码来源:TypeSystem.cs

示例10: GenerateTypeParamElement

        private void GenerateTypeParamElement(IProcessingContext context, MemberInfo mInfo, Type tp)
        {
            var tpElem = new XElement("typeparam", new XAttribute("name", tp.Name));

            if (tp.GenericParameterAttributes.HasFlag(GenericParameterAttributes.Contravariant))
                tpElem.Add(new XAttribute("isContravariant", XmlConvert.ToString(true)));

            if (tp.GenericParameterAttributes.HasFlag(GenericParameterAttributes.Covariant))
                tpElem.Add(new XAttribute("isCovariant", XmlConvert.ToString(true)));

            if (tp.GenericParameterAttributes.HasFlag(GenericParameterAttributes.NotNullableValueTypeConstraint))
                tpElem.Add(new XAttribute("isValueType", XmlConvert.ToString(true)));

            if (tp.GenericParameterAttributes.HasFlag(GenericParameterAttributes.ReferenceTypeConstraint))
                tpElem.Add(new XAttribute("isReferenceType", XmlConvert.ToString(true)));

            if (tp.GenericParameterAttributes.HasFlag(GenericParameterAttributes.DefaultConstructorConstraint))
                tpElem.Add(new XAttribute("hasDefaultConstructor", XmlConvert.ToString(true)));

            context.Element.Add(tpElem);

            foreach (Type constraint in tp.GetGenericParameterConstraints())
            {
                var ctElement = new XElement("constraint");
                tpElem.Add(ctElement);
                GenerateTypeRef(context.Clone(ctElement), constraint);
            }

            // enrich typeparam
            foreach (IEnricher enricher in this.Enrichers)
                enricher.EnrichTypeParameter(context.Clone(tpElem), tp);
        }
开发者ID:LBiNetherlands,项目名称:LBi.LostDoc,代码行数:32,代码来源:DocGenerator.cs

示例11: AddConstraintsFromType

		internal static void AddConstraintsFromType(ITypeParameter tp, Type type)
		{
			foreach (Type constraint in type.GetGenericParameterConstraints()) {
				if (tp.Method != null) {
					tp.Constraints.Add(ReflectionReturnType.Create(tp.Method, constraint));
				} else {
					tp.Constraints.Add(ReflectionReturnType.Create(tp.Class, constraint));
				}
			}
		}
开发者ID:SAD1992,项目名称:justdecompile-plugins,代码行数:10,代码来源:ReflectionClass.cs

示例12: ImportGenericType

 private GenericSymbol ImportGenericType(Type type)
 {
     var attribute = new List<AttributeSymbol>();
     AppendEmbededAttribute(attribute, type);
     var constraint = CreateConstraintList(type.GetGenericParameterConstraints());
     var elem = new GenericSymbol(type.Name, attribute, constraint);
     if (ImportDictionary.ContainsKey(type))
     {
         return (GenericSymbol)ImportDictionary[type];
     }
     ImportDictionary.Add(type, elem);
     return elem;
 }
开发者ID:B-head,项目名称:Dreit-prototype,代码行数:13,代码来源:CilImport.cs

示例13: TrySimple

        /// <summary>
        /// Tries a simple solution to to get a non generic type by using either a built in type or the constraint type
        /// if there is only one.
        /// </summary>
        /// <param name="genericType">The generic type.</param>
        /// <param name="nonGenericType">The non generic type for the <paramref name="genericType"/>.</param>
        /// <returns><see langword="true"/> if a <paramref name="nonGenericType"/> can be used; otherwise
        /// <see langword="false"/>.</returns>
        /// <exception cref="ArgumentNullException">The <paramref name="genericType"/> parameter is
        /// <see langword="null"/>.</exception>
        private static bool TrySimple(Type genericType, out Type nonGenericType)
        {
            if (genericType == null)
                throw new ArgumentNullException("genericType");

            Type[] constraints = genericType.GetGenericParameterConstraints();
            GenericParameterAttributes attributes = genericType.GenericParameterAttributes;

            // Handle the simple situation where there are no constraints.
            if (constraints.Length == 0)
            {
                if (attributes == GenericParameterAttributes.None
                    || attributes.HasFlag(GenericParameterAttributes.ReferenceTypeConstraint))
                {
                    nonGenericType = typeof(object);
                    return true;
                }
            }

            // Handle the simple situation where the single constraint.
            if (constraints.Length == 1)
            {
                Type constraint = constraints[0];
                if (constraint == typeof(ValueType))
                {
                    nonGenericType = typeof(int);
                    return true;
                }

                // If there is a single constraint with no attributes, just use the constraint as the non generic type.
                if (attributes == GenericParameterAttributes.None)
                {
                    nonGenericType = constraint;
                    return true;
                }

                // If there is a single constraint, the generic type requires a default constructor and the constraint
                // has a default constructor, just use the constraint as the non generic type.
                if (attributes == GenericParameterAttributes.DefaultConstructorConstraint)
                {
                    if (constraint.GetConstructor(Type.EmptyTypes) != null)
                    {
                        nonGenericType = constraint;
                        return true;
                    }
                }
            }

            nonGenericType = null;
            return false;
        }
开发者ID:AutoTestNET,项目名称:AutoTest.ArgumentNullException,代码行数:61,代码来源:GenericTypeConversion.cs

示例14: AreTypesEquivalent

        /// <summary>
        /// Verifies that two types are equivalent.
        /// </summary>
        /// <param name="actual">The actual type.</param>
        /// <param name="expected">The expected type.</param>
        private static void AreTypesEquivalent(Type actual, Type expected)
        {
            // Check type properties.
            Assert.That(actual.IsAbstract, Is.EqualTo(expected.IsAbstract));
            Assert.That(actual.IsAnsiClass, Is.EqualTo(expected.IsAnsiClass));
            Assert.That(actual.IsArray, Is.EqualTo(expected.IsArray));
            Assert.That(actual.IsAutoClass, Is.EqualTo(expected.IsAutoClass));
            Assert.That(actual.IsAutoLayout, Is.EqualTo(expected.IsAutoLayout));
            Assert.That(actual.IsByRef, Is.EqualTo(expected.IsByRef));
            Assert.That(actual.IsClass, Is.EqualTo(expected.IsClass));
            Assert.That(actual.IsCOMObject, Is.EqualTo(expected.IsCOMObject));
            Assert.That(actual.IsContextful, Is.EqualTo(expected.IsContextful));
            Assert.That(actual.IsEnum, Is.EqualTo(expected.IsEnum));
            Assert.That(actual.IsExplicitLayout, Is.EqualTo(expected.IsExplicitLayout));
            Assert.That(actual.IsInterface, Is.EqualTo(expected.IsInterface));
            Assert.That(actual.IsLayoutSequential, Is.EqualTo(expected.IsLayoutSequential));
            Assert.That(actual.IsMarshalByRef, Is.EqualTo(expected.IsMarshalByRef));
            Assert.That(actual.IsNested, Is.EqualTo(expected.IsNested));
            Assert.That(actual.IsNestedAssembly, Is.EqualTo(expected.IsNestedAssembly));
            Assert.That(actual.IsNestedFamANDAssem, Is.EqualTo(expected.IsNestedFamANDAssem));
            Assert.That(actual.IsNestedFamily, Is.EqualTo(expected.IsNestedFamily));
            Assert.That(actual.IsNestedFamORAssem, Is.EqualTo(expected.IsNestedFamORAssem));
            Assert.That(actual.IsNestedPrivate, Is.EqualTo(expected.IsNestedPrivate));
            Assert.That(actual.IsNestedPublic, Is.EqualTo(expected.IsNestedPublic));
            Assert.That(actual.IsNotPublic, Is.EqualTo(expected.IsNotPublic));
            Assert.That(actual.IsPointer, Is.EqualTo(expected.IsPointer));
            Assert.That(actual.IsPrimitive, Is.EqualTo(expected.IsPrimitive));
            Assert.That(actual.IsPublic, Is.EqualTo(expected.IsPublic));
            Assert.That(actual.IsSealed, Is.EqualTo(expected.IsSealed));
            //Assert.That(actual.IsSecuritySafeCritical, Is.EqualTo(expected.IsSecuritySafeCritical));
            Assert.That(actual.IsSerializable, Is.EqualTo(expected.IsSerializable));
            Assert.That(actual.IsSpecialName, Is.EqualTo(expected.IsSpecialName));
            Assert.That(actual.IsUnicodeClass, Is.EqualTo(expected.IsUnicodeClass));
            Assert.That(actual.IsValueType, Is.EqualTo(expected.IsValueType));
            Assert.That(actual.IsVisible, Is.EqualTo(expected.IsVisible));
            Assert.That(actual.MemberType, Is.EqualTo(expected.MemberType));
            Assert.That(actual.Name, Is.EqualTo(expected.Name));
            Assert.That(actual.StructLayoutAttribute, Is.EqualTo(expected.StructLayoutAttribute));
            Assert.That(actual.IsGenericType, Is.EqualTo(expected.IsGenericType));

            // Ignore .NET Framework 4 properties.
            //Assert.That(actual.IsSecurityCritical, Is.EqualTo(expected.IsSecurityCritical));
            //Assert.That(actual.IsSecurityTransparent, Is.EqualTo(expected.IsSecurityTransparent));

            // Check type attributes.
            Assert.That(actual.Attributes, Is.EqualTo(expected.Attributes));

            // Check element type.
            Assert.That(actual.HasElementType, Is.EqualTo(expected.HasElementType));

            if (actual.HasElementType)
                AreTypesEquivalent(actual.GetElementType(), expected.GetElementType());

            // Check generic parameters.
            Assert.That(actual.ContainsGenericParameters, Is.EqualTo(expected.ContainsGenericParameters));

            if (actual.ContainsGenericParameters)
                AreTypesEquivalent(actual.GetGenericArguments(), expected.GetGenericArguments());

            // Check generic parameter.
            Assert.That(actual.IsGenericParameter, Is.EqualTo(expected.IsGenericParameter));

            if (actual.IsGenericParameter)
            {
                Assert.That(actual.GenericParameterAttributes, Is.EqualTo(expected.GenericParameterAttributes));
                AreTypesEquivalent(actual.GetGenericParameterConstraints(), expected.GetGenericParameterConstraints());
                Assert.That(actual.GenericParameterPosition, Is.EqualTo(expected.GenericParameterPosition));
            }

            // Check generic type definition.
            Assert.That(actual.IsGenericTypeDefinition, Is.EqualTo(expected.IsGenericTypeDefinition));

            if (actual.IsGenericTypeDefinition)
                AreTypesEquivalent(actual.GetGenericTypeDefinition(), expected.GetGenericTypeDefinition());
        }
开发者ID:netcasewqs,项目名称:nlite,代码行数:80,代码来源:MemberAssert.cs

示例15: CreateGenericTypeParameter

            private CodeDocGenericParameter CreateGenericTypeParameter(Type genericArgument, ICodeDocMemberDataProvider provider)
            {
                Contract.Requires(genericArgument != null);
                Contract.Requires(provider != null);
                Contract.Ensures(Contract.Result<CodeDocGenericParameter>() != null);
                var argumentName = genericArgument.Name;
                Contract.Assume(!String.IsNullOrEmpty(argumentName));
                var typeConstraints = genericArgument.GetGenericParameterConstraints();
                var model = new CodeDocGenericParameter(argumentName);

                model.SummaryContents = provider
                    .GetGenericTypeSummaryContents(argumentName)
                    .ToArray();

                if (typeConstraints.Length > 0)
                    model.TypeConstraints = Array.ConvertAll(typeConstraints, t => GetOrConvert(t, CodeDocMemberDetailLevel.Minimum));

                model.IsContravariant = genericArgument.GenericParameterAttributes.HasFlag(
                    GenericParameterAttributes.Contravariant);
                model.IsCovariant = genericArgument.GenericParameterAttributes.HasFlag(
                    GenericParameterAttributes.Covariant);
                model.HasDefaultConstructorConstraint = genericArgument.GenericParameterAttributes.HasFlag(
                    GenericParameterAttributes.DefaultConstructorConstraint);
                model.HasNotNullableValueTypeConstraint = genericArgument.GenericParameterAttributes.HasFlag(
                    GenericParameterAttributes.NotNullableValueTypeConstraint);
                model.HasReferenceTypeConstraint = genericArgument.GenericParameterAttributes.HasFlag(
                    GenericParameterAttributes.ReferenceTypeConstraint);

                return model;
            }
开发者ID:aarondandy,项目名称:ducky-docs,代码行数:30,代码来源:ReflectionCodeDocMemberRepository.cs


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