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


C# Type.GetInterface方法代码示例

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


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

示例1: ParameterCollectionTypeInformation

        /// <summary>
        /// Constructs a parameter collection type information object
        /// which exposes the specified Type's collection type in a
        /// simple way.
        /// </summary>
        /// 
        /// <param name="type">
        /// The type to determine the collection information for.
        /// </param>
        /// 
        internal ParameterCollectionTypeInformation(Type type)
        {
            ParameterCollectionType = ParameterCollectionType.NotCollection;
            Diagnostics.Assert(type != null, "Caller to verify type argument");
            TypeInfo typeInfo = type.GetTypeInfo();

            // NTRAID#Windows OS Bugs-1009284-2004/05/11-JeffJon
            // What other collection types should be supported?

            // Look for array types

            // NTRAID#Windows Out of Band Releases-906820-2005/09/07
            // According to MSDN, IsSubclassOf returns false if the types are exactly equal.
            // Should this include ==?
            if (type.IsSubclassOf(typeof(Array)))
            {
                ParameterCollectionType = ParameterCollectionType.Array;
                ElementType = type.GetElementType();
                return;
            }

            if (typeof(IDictionary).IsAssignableFrom(type))
            {
                return;
            }

            Type[] interfaces = type.GetInterfaces();
            if (interfaces.Any(i => i.GetTypeInfo().IsGenericType && i.GetGenericTypeDefinition() == typeof(IDictionary<,>))
                || (typeInfo.IsGenericType && type.GetGenericTypeDefinition() == typeof(IDictionary<,>)))
            {
                return;
            }

            bool implementsIList = (type.GetInterface(typeof(IList).Name) != null);

            // Look for class Collection<T>.  Collection<T> implements IList, and also IList
            // is more efficient to bind than ICollection<T>.  This optimization
            // retrieves the element type so that we can coerce the elements.
            // Otherwise they must already be the right type.
            if (implementsIList && typeInfo.IsGenericType && (type.GetGenericTypeDefinition() == typeof(Collection<>)))
            {
                ParameterCollectionType = ParameterCollectionType.IList;
                // figure out elementType
                Type[] elementTypes = type.GetGenericArguments();
                Diagnostics.Assert(
                    elementTypes.Length == 1,
                    "Expected 1 generic argument, got " + elementTypes.Length);
                ElementType = elementTypes[0];
                return;
            }

            // Look for interface ICollection<T>.  Note that Collection<T>
            // does not implement ICollection<T>, and also, ICollection<T>
            // does not derive from IList.  The only way to add elements
            // to an ICollection<T> is via reflected calls to Add(T),
            // but the advantage over plain IList is that we can typecast the elements.
            Type interfaceICollection =
                interfaces.FirstOrDefault(i => i.GetTypeInfo().IsGenericType && i.GetGenericTypeDefinition() == typeof(ICollection<>));
            if (interfaceICollection != null)
            {
                // We only deal with the first type for which ICollection<T> is implemented
                ParameterCollectionType = ParameterCollectionType.ICollectionGeneric;
                // figure out elementType
                Type[] elementTypes = interfaceICollection.GetGenericArguments();
                Diagnostics.Assert(
                    elementTypes.Length == 1,
                    "Expected 1 generic argument, got " + elementTypes.Length);
                ElementType = elementTypes[0];
                return;
            }

            // Look for IList
            if (implementsIList)
            {
                ParameterCollectionType = ParameterCollectionType.IList;
                // elementType remains null
                return;
            }
        } // ctor
开发者ID:40a,项目名称:PowerShell,代码行数:89,代码来源:CompiledCommandParameter.cs

示例2: IsActivityTemplateFactory

 private static bool IsActivityTemplateFactory(Type type)
 {
     return type.GetInterface(typeof(IActivityTemplateFactory).FullName) != null ||
            type.GetInterface(typeof(IActivityTemplateFactory<>).FullName) != null;
 }
开发者ID:nlh774,项目名称:DotNetReferenceSource,代码行数:5,代码来源:FlowchartDesigner.xaml.cs

示例3: GetSetElementType

        // <summary>
        // Given a type that might be an IDbSet\IObjectSet, determine if the type implements IDbSet&lt;&gt;\IObjectSet&lt;&gt;, and if
        // so return the element type of the IDbSet\IObjectSet.  Currently, if the collection implements IDbSet&lt;&gt;\IObjectSet&lt;&gt;
        // multiple times with different types, then we will return false since this is not supported.
        // </summary>
        // <param name="setType"> The type to check. </param>
        // <returns> The element type of the IDbSet\IObjectSet, or null if the type does not match. </returns>
        private static Type GetSetElementType(Type setType)
        {
            // We have to check if the type actually is the interface, or if it implements the interface:
            try
            {
                var setInterface =
                    (setType.IsGenericType() && typeof(IDbSet<>).IsAssignableFrom(setType.GetGenericTypeDefinition()))
                        ? setType
                        : setType.GetInterface(typeof(IDbSet<>).FullName);

                // We need to make sure the type is fully specified otherwise we won't be able to add element to it.
                if (setInterface != null
                    && !setInterface.ContainsGenericParameters())
                {
                    return setInterface.GetGenericArguments()[0];
                }
            }
            catch (AmbiguousMatchException)
            {
                // Thrown if collection type implements IDbSet or IObjectSet<> more than once
            }
            return null;
        }
开发者ID:Cireson,项目名称:EntityFramework6,代码行数:30,代码来源:DbSetDiscoveryService.cs

示例4: ObjectMemberAccessor

        private ObjectMemberAccessor(Type type)
        {
            /*
            if ( !TypeUtils.IsPublic(type) )
                throw new ArgumentException(
                    "Can not serialize non-public type {0}.".DoFormat(type.FullName));
            */

            const BindingFlags propertyFlags = BindingFlags.Instance | BindingFlags.Public | BindingFlags.GetProperty | BindingFlags.NonPublic;

            // public properties
            foreach ( var p in type.GetProperties(propertyFlags) ) {
                var prop = p; // create closures with this local variable
                // not readable or parameters required to access the property
                if ( !prop.CanRead || prop.GetGetMethod(false) == null || prop.GetIndexParameters().Count() != 0 )
                    continue;
                Func<object, object> get = obj => prop.GetValue(obj, EmptyObjectArray);
                Action<object, object> set = null;
                if ( prop.CanWrite && prop.GetSetMethod(false) != null )
                    set = (obj, value) => prop.SetValue(obj, value, EmptyObjectArray);
                RegisterMember(type, prop, prop.PropertyType, get, set);
            }

            const BindingFlags fieldFlags = BindingFlags.Instance | BindingFlags.Public | BindingFlags.GetField | BindingFlags.NonPublic;

            // public fields
            foreach ( var f in type.GetFields(fieldFlags) ) {
                var field = f;
                Func<object, object> get = field.GetValue;
                Action<object, object> set = field.SetValue;
                RegisterMember(type, field, field.FieldType, get, set);
            }

            Type itype;

            // implements IDictionary
            if ( type.GetInterface("System.Collections.IDictionary") != null ) {
                IsDictionary = true;
                IsReadOnly = obj => ( (System.Collections.IDictionary)obj ).IsReadOnly;
                // extract Key, Value types from IDictionary<??, ??>
                itype = type.GetInterface("System.Collections.Generic.IDictionary`2");
                if ( itype != null ) {
                    KeyType = itype.GetGenericArguments()[0];
                    ValueType = itype.GetGenericArguments()[1];
                }
            } else
                // implements ICollection<T>
                if ( ( itype = type.GetInterface("System.Collections.Generic.ICollection`1") ) != null ) {
                    ValueType = itype.GetGenericArguments()[0];
                    var add = itype.GetMethod("Add", new Type[] { ValueType });
                    CollectionAdd = (obj, value) => add.Invoke(obj, new object[] { value });
                    var clear = itype.GetMethod("Clear", new Type[0]);
                    CollectionClear = obj => clear.Invoke(obj, new object[0]);
                    var isReadOnly = itype.GetProperty("IsReadOnly", new Type[0]).GetGetMethod();
                    IsReadOnly = obj => (bool)isReadOnly.Invoke(obj, new object[0]);
                } else
                    // implements IList
                    if ( ( itype = type.GetInterface("System.Collections.IList") ) != null ) {
                        var add = itype.GetMethod("Add", new Type[] { typeof(object) });
                        CollectionAdd = (obj, value) => add.Invoke(obj, new object[] { value });
                        var clear = itype.GetMethod("Clear", new Type[0]);
                        CollectionClear = obj => clear.Invoke(obj, new object[0]);
                        /* IList<T> implements ICollection<T>
                        // Extract Value Type from IList<T>
                        itype = type.GetInterface("System.Collections.Generic.IList`1");
                        if ( itype != null )
                            ValueType = itype.GetGenericArguments()[0];
                         */
                        IsReadOnly = obj => ((System.Collections.IList)obj).IsReadOnly;
                    }
        }
开发者ID:vfioox,项目名称:ENULib,代码行数:71,代码来源:ObjectMemberAccessor.cs


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