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


C# Type.GetInterfaces方法代码示例

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


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

示例1: checkBaseAndInterfaces

  private static void checkBaseAndInterfaces(Type cls, bool interfaceExpected, string baseClass, string[] interfaces) {
    string[] expectedInterfaces = new string[interfaces.Length + (interfaceExpected ? 0 : 1)];
    for (int i=0; i<interfaces.Length; ++i)
      expectedInterfaces[i] = interfaces[i];
    if (!interfaceExpected)
      expectedInterfaces[interfaces.Length] = "IDisposable";
    Type[] actualInterfaces = cls.GetInterfaces();
    string expectedInterfacesString = SortArrayToString(expectedInterfaces);
    string actualInterfacesString = SortArrayToString(actualInterfaces);
    if (expectedInterfacesString != actualInterfacesString)
      throw new Exception("Expected interfaces for " + cls.Name + ": \n" + expectedInterfacesString + "\n" + "Actual interfaces: \n" + actualInterfacesString);

    string expectedBaseString = null;
    if (interfaceExpected) {
      // expecting an interface
      if (!cls.IsInterface)
        throw new Exception(cls.Name + " should be an interface but is not");
      expectedBaseString = string.IsNullOrEmpty(baseClass) ? "" : "multiple_inheritance_interfacesNamespace." + baseClass;
    } else {
      // expecting a class
      if (cls.IsInterface)
        throw new Exception(cls.Name + " is an interface but it should not be");
      expectedBaseString = string.IsNullOrEmpty(baseClass) ? "Object" : baseClass;
    }

    string actualBaseString = cls.BaseType == null ? "" : cls.BaseType.Name;
    if (expectedBaseString != actualBaseString)
      throw new Exception("Expected base for " + cls.Name + ": [" + expectedBaseString + "]" + " Actual base: [" + actualBaseString + "]");
  }
开发者ID:tamuratak,项目名称:swig,代码行数:29,代码来源:multiple_inheritance_interfaces_runme.cs

示例2: GetImplementedIEnumerableType

        /// <summary>
        /// Returns type of T if the type implements IEnumerable of T, otherwise, return null.
        /// </summary>
        /// <param name="type"></param>
        /// <returns></returns>
        internal static Type GetImplementedIEnumerableType(Type type)
        {
            if (type.IsGenericType && type.IsInterface && 
                (type.GetGenericTypeDefinition() == typeof(IEnumerable<>) || 
                 type.GetGenericTypeDefinition() == typeof(IQueryable<>)))
            {
                // special case the IEnumerable<T>
                return GetInnerGenericType(type);
            }
            else
            {
                // for the rest of interfaces and strongly Type collections
                Type[] interfaces = type.GetInterfaces();
                foreach (Type interfaceType in interfaces)
                {
                    if (interfaceType.IsGenericType && 
                        (interfaceType.GetGenericTypeDefinition() == typeof(IEnumerable<>) ||
                         interfaceType.GetGenericTypeDefinition() == typeof(IQueryable<>)))
                    {
                        // special case the IEnumerable<T>
                        return GetInnerGenericType(interfaceType);
                    }
                }
            }

            return null;
        }
开发者ID:mikevpeters,项目名称:aspnetwebstack,代码行数:32,代码来源:TypeHelper.cs

示例3: ListInterfaces

 static void ListInterfaces(Type t)
 {
     Console.WriteLine("***** Interfaces *****");
     var ifaces = from i in t.GetInterfaces() select i;
     foreach (var i in ifaces) {
         Console.WriteLine("->{0}", i.Name);
     }
 }
开发者ID:walrus7521,项目名称:code,代码行数:8,代码来源:Reflection.cs

示例4: ArgumentCompleterAttribute

        /// <param name="type">The type must implement <see cref="IArgumentCompleter"/> and have a default constructor.</param>
        public ArgumentCompleterAttribute(Type type)
        {
            if (type == null || (type.GetInterfaces().All(t => t != typeof(IArgumentCompleter))))
            {
                throw PSTraceSource.NewArgumentException("type");
            }

            Type = type;
        }
开发者ID:40a,项目名称:PowerShell,代码行数:10,代码来源:ExtensibleCompletion.cs

示例5: ExtractGenericInterface

 public static Type ExtractGenericInterface(Type queryType, Type interfaceType)
 {
     Func<Type, bool> predicate = t => t.IsGenericType && (t.GetGenericTypeDefinition() == interfaceType);
     if(!predicate(queryType))
     {
         return queryType.GetInterfaces().FirstOrDefault(predicate);
     }
     return queryType;
 }
开发者ID:DigitalOkha,项目名称:MVC-3-Html5EditorFor,代码行数:9,代码来源:Html5TypeHelpers.cs

示例6: GetNonDynamicType

 private static Type GetNonDynamicType(Type type)
 {
     var baseType = type.BaseType;
     if (baseType == typeof(object))
     {
         var interfaces = type.GetInterfaces();
         if (interfaces.Length <= 1)
             throw new Exception(string.Format("Could not create non dynamic type for '{0}'.", type.FullName));
         return GetNonProxiedType(interfaces[0]);
     }
     return GetNonProxiedType(baseType);
 }
开发者ID:hobdrive,项目名称:NHaml,代码行数:12,代码来源:ProxyExtracter.cs

示例7: FindGenericEnumerableType

 public static Type FindGenericEnumerableType(Type type) {
     // Logic taken from Queryable.AsQueryable which accounts for Array types which are not
     // generic but implement the generic IEnumerable interface.
     while ((type != null) && (type != typeof(object)) && (type != typeof(string))) {
         if (type.IsGenericType && (type.GetGenericTypeDefinition() == typeof(IEnumerable<>))) {
             return type;
         }
         foreach (Type interfaceType in type.GetInterfaces()) {
             Type genericInterface = FindGenericEnumerableType(interfaceType);
             if (genericInterface != null) {
                 return genericInterface;
             }
         }
         type = type.BaseType;
     }
     return null;
 }
开发者ID:iskiselev,项目名称:JSIL.NetFramework,代码行数:17,代码来源:LinqDataSourceHelper.cs

示例8: IsTypeCompatible

        public static bool IsTypeCompatible(Type childObjectType, Type parentObjectType)
        {
            if (!parentObjectType.IsGenericTypeDefinition)
            {
                return parentObjectType.IsAssignableFrom(childObjectType);
            }
            else if (parentObjectType.IsInterface)
            {
                Type[] interfaceTypes = childObjectType.GetInterfaces();
                foreach (Type interfaceType in interfaceTypes)
                {
                    if (interfaceType.IsGenericType)
                    {
                        if (interfaceType.GetGenericTypeDefinition() == parentObjectType)
                        {
                            return true;
                        }
                    }
                }

                return false;
            }
            else
            {
                Type current = childObjectType;
                while (current != null)
                {
                    if (current.IsGenericType)
                    {
                        if (current.GetGenericTypeDefinition() == parentObjectType)
                        {
                            return true;
                        }
                    }

                    current = current.BaseType;
                }

                return false;
            }
        }
开发者ID:iskiselev,项目名称:JSIL.NetFramework,代码行数:41,代码来源:TypeUtilities.cs

示例9: FindGenericType

 private static Type FindGenericType(Type definition, Type type)
 {
     while ((type != null) && (type != typeof(object)))
     {
         if (type.IsGenericType && (type.GetGenericTypeDefinition() == definition))
         {
             return type;
         }
         if (definition.IsInterface)
         {
             foreach (Type type2 in type.GetInterfaces())
             {
                 Type type3 = FindGenericType(definition, type2);
                 if (type3 != null)
                 {
                     return type3;
                 }
             }
         }
         type = type.BaseType;
     }
     return null;
 }
开发者ID:yannduran,项目名称:Twainsoft.StudioStyler,代码行数:23,代码来源:TypeHelper.cs

示例10: FindEditorInTable

	private static object FindEditorInTable (Type componentType, Type editorBaseType, Hashtable table)
	{
		object editorReference = null;
		object editor = null;
		
		if (componentType == null || editorBaseType == null || table == null)
			return null;
			
		Type ct = componentType;
		while (ct != null) {						
			editorReference = table [ct];
			if (editorReference != null)
				break;			
			ct = ct.BaseType;
		}
		
		if (editorReference == null) {
			foreach (Type iface in componentType.GetInterfaces ()) {
				editorReference = table [iface];
				if (editorReference != null) 
					break;
			}
		}
				
		if (editorReference == null)
			return null;
				
		if (editorReference is string)
			editor = CreateEditor (Type.GetType ((string) editorReference), componentType);
		else if (editorReference is Type)
			editor = CreateEditor ((Type) editorReference, componentType);
		else if (editorReference.GetType ().IsSubclassOf (editorBaseType))
			editor = editorReference;
		
		if (editor != null) 
			table [componentType] = editor;
		
		return editor;
	}
开发者ID:calumjiao,项目名称:Mono-Class-Libraries,代码行数:39,代码来源:TypeDescriptor.cs

示例11: IsValidInstanceType

 // Checks if the type is a valid target for an instance call
 internal static bool IsValidInstanceType(MemberInfo member, Type instanceType) {
     Type targetType = member.DeclaringType;
     if (AreReferenceAssignable(targetType, instanceType)) {
         return true;
     }
     if (instanceType.IsValueType) {
         if (AreReferenceAssignable(targetType, typeof(System.Object))) {
             return true;
         }
         if (AreReferenceAssignable(targetType, typeof(System.ValueType))) {
             return true;
         }
         if (instanceType.IsEnum && AreReferenceAssignable(targetType, typeof(System.Enum))) {
             return true;
         }
         // A call to an interface implemented by a struct is legal whether the struct has
         // been boxed or not.
         if (targetType.IsInterface) {
             foreach (Type interfaceType in instanceType.GetInterfaces()) {
                 if (AreReferenceAssignable(targetType, interfaceType)) {
                     return true;
                 }
             }
         }
     }
     return false;
 }
开发者ID:krytht,项目名称:DotNetReferenceSource,代码行数:28,代码来源:TypeUtils.cs

示例12: TypeGetInterfaces

	static Type [] TypeGetInterfaces (Type t, bool declonly)
	{
		Type [] ifaces = t.GetInterfaces ();
		if (! declonly)
			return ifaces;

		// Handle Object. Also, optimize for no interfaces
		if (t.BaseType == null || ifaces.Length == 0)
			return ifaces;

		ArrayList ar = new ArrayList ();

		foreach (Type i in ifaces)
			if (! i.IsAssignableFrom (t.BaseType))
				ar.Add (i);

		return (Type []) ar.ToArray (typeof (Type));
	}
开发者ID:alesliehughes,项目名称:olive,代码行数:18,代码来源:outline.cs

示例13: CompareCloserType

			int CompareCloserType (Type t1, Type t2)
			{
				if (t1 == t2)
					return 0;
				if (t1.IsGenericParameter && !t2.IsGenericParameter)
					return 1; // t2
				if (!t1.IsGenericParameter && t2.IsGenericParameter)
					return -1; // t1
				if (t1.HasElementType && t2.HasElementType)
					return CompareCloserType (
						t1.GetElementType (),
						t2.GetElementType ());

				if (t1.IsSubclassOf (t2))
					return -1; // t1
				if (t2.IsSubclassOf (t1))
					return 1; // t2

				if (t1.IsInterface && Array.IndexOf (t2.GetInterfaces (), t1) >= 0)
					return 1; // t2
				if (t2.IsInterface && Array.IndexOf (t1.GetInterfaces (), t2) >= 0)
					return -1; // t1

				// What kind of cases could reach here?
				return 0;
			}
开发者ID:ramonsmits,项目名称:mono,代码行数:26,代码来源:Binder.cs

示例14: ImplementsInterface

 private static bool ImplementsInterface(Type type, Type targetInterfaceType)
 {
     Func<Type, bool> implementsInterface = t => targetInterfaceType.IsAssignableFrom(t);
     if (targetInterfaceType.IsGenericType)
     {
         implementsInterface = t => t.IsGenericType && targetInterfaceType.IsAssignableFrom(t.GetGenericTypeDefinition());
     }
     return implementsInterface(type) || type.GetInterfaces().Any(implementsInterface);
 }
开发者ID:AndreGleichner,项目名称:aspnetwebstack,代码行数:9,代码来源:ObjectVisitor.cs

示例15: ClassGen

	static void ClassGen (Type t)
	{
		//TODO: what flags do we want for GetEvents and GetConstructors?

		//events as signals
		EventInfo[] events;
		events = t.GetEvents (BindingFlags.Public|BindingFlags.Instance|BindingFlags.DeclaredOnly);

		//events as signals
		MethodInfo[] methods;
		methods = t.GetMethods (BindingFlags.Public|BindingFlags.Instance|BindingFlags.DeclaredOnly);

		Type[] ifaces;
		ifaces = t.GetInterfaces ();

		H.WriteLine ("G_BEGIN_DECLS");
		H.WriteLine ();

		{
			string NS = NsToC (ns).ToUpper ();
			string T = CamelToC (t.Name).ToUpper ();
			string NST = NS + "_" + T;
			string NSTT = NS + "_TYPE_" + T;

			H.WriteLine ("#define " + NSTT + " (" + cur_type + "_get_type ())");
			H.WriteLine ("#define " + NST + "(object) (G_TYPE_CHECK_INSTANCE_CAST ((object), " + NSTT + ", " + CurType + "))");
			if (!t.IsInterface)
				H.WriteLine ("#define " + NST + "_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), " + NSTT + ", " + CurTypeClass + "))");
			H.WriteLine ("#define " + NS + "_IS_" + T + "(object) (G_TYPE_CHECK_INSTANCE_TYPE ((object), " + NSTT + "))");
			if (!t.IsInterface)
				H.WriteLine ("#define " + NS + "_IS_" + T + "_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), " + NSTT + "))");
			if (t.IsInterface)
				H.WriteLine ("#define " + NST + "_GET_INTERFACE(obj) (G_TYPE_INSTANCE_GET_INTERFACE ((obj), " + NSTT + ", " + CurTypeClass + "))");
			else
				H.WriteLine ("#define " + NST + "_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), " + NSTT + ", " + CurTypeClass + "))");
		}

		if (!C.IsDuplicate) {
			Hdecls.WriteLine ("typedef struct _" + CurType + " " + CurType + ";");
			Hdecls.WriteLine ("typedef struct _" + CurTypeClass + " " + CurTypeClass + ";");
			Hdecls.WriteLine ();
		}

		H.WriteLine ();

		string ParentName;
		string ParentNameClass;

		if (t.BaseType != null) {
			ParentName = CsTypeToG (t.BaseType);
			ParentNameClass = GToGC (ParentName);
		} else {
			ParentName = "GType";
			if (t.IsInterface)
				ParentNameClass = ParentName + "Interface";
			else
				ParentNameClass = ParentName + "Class";
		}

		//H.WriteLine ("typedef struct _" + CurType + " " + CurType + ";");

		//H.WriteLine ();
		//H.WriteLine ("typedef struct _" + CurType + "Class " + CurType + "Class;");
		if (!t.IsInterface) {
			H.WriteLine ("typedef struct _" + CurType + "Private " + CurType + "Private;");
			H.WriteLine ();
			H.WriteLine ("struct _" + CurType);
			H.WriteLine ("{");

			H.WriteLine (ParentName + " parent_instance;");
			H.WriteLine (CurType + "Private *priv;");
			H.WriteLine ("};");
			H.WriteLine ();
		}

		H.WriteLine ("struct _" + CurTypeClass);
		H.WriteLine ("{");
		//H.WriteLine (ParentNameClass + " parent_class;");
		H.WriteLine (ParentNameClass + " parent;");
		if (t.BaseType != null)
			H.WriteLine ("/* inherits " + t.BaseType.Namespace + " " + t.BaseType.Name + " */");

		if (events.Length != 0) {
			H.WriteLine ();
			H.WriteLine ("/* signals */");

			//FIXME: event arguments
			foreach (EventInfo ei in events)
				H.WriteLine ("void (* " + CamelToC (ei.Name) + ") (" + CurType + " *thiz" + ");");
		}

		if (t.IsInterface) {
			if (methods.Length != 0) {
				H.WriteLine ();
				H.WriteLine ("/* vtable */");

				//FIXME: method arguments
				//string funcname = ToValidFuncName (CamelToC (imi.Name));
				foreach (MethodInfo mi in methods)
					H.WriteLine ("void (* " + CamelToC (mi.Name) + ") (" + CurType + " *thiz" + ");");
//.........这里部分代码省略.........
开发者ID:calumjiao,项目名称:Mono-Class-Libraries,代码行数:101,代码来源:cilc.cs


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