本文整理汇总了C#中System.Type.GetInterface方法的典型用法代码示例。如果您正苦于以下问题:C# Type.GetInterface方法的具体用法?C# Type.GetInterface怎么用?C# Type.GetInterface使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Type
的用法示例。
在下文中一共展示了Type.GetInterface方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: GetPerRequestFormatterInstance
public override MediaTypeFormatter GetPerRequestFormatterInstance(Type type, HttpRequestMessage request,MediaTypeHeaderValue mediaType)
{
//所需的对象属性
var includingFields = request.GetRouteData().Values["fields"];
if (includingFields != null && !string.IsNullOrEmpty(includingFields.ToString()))
{
FieldsJsonMediaTypeFormatter frmtr = new FieldsJsonMediaTypeFormatter();
frmtr.CurrentRequest = request;
var resolve = new Share.IncludableSerializerContractResolver(this.SerializerSettings.ContractResolver as Share.IgnorableSerializerContractResolver);
//type.IsAssignableFrom(typeof(IEnumerable<Model.dr_pre_visit>))
if (type.GetInterface("IEnumerable") != null)
{
resolve.Include(type.GenericTypeArguments[0], includingFields.ToString(), ',');
}
else
{
resolve.Include(type, includingFields.ToString(), ",");
}
frmtr.SerializerSettings = new JsonSerializerSettings
{
ContractResolver = resolve,
};
return frmtr;
}
else
{
return this;
}
}
示例2: ConvertToFromVector4
public static object ConvertToFromVector4(ITypeDescriptorContext context, CultureInfo culture, Vector4 value, Type destinationType)
{
if (destinationType == typeof(float))
{
return value.X;
}
if (destinationType == typeof(Vector2))
{
return new Vector2(value.X, value.Y);
}
if (destinationType == typeof(Vector3))
{
return new Vector3(value.X, value.Y, value.Z);
}
if (destinationType == typeof(Vector4))
{
return new Vector4(value.X, value.Y, value.Z, value.W);
}
if (destinationType.GetInterface("IPackedVector") != null)
{
IPackedVector packedVec = (IPackedVector) Activator.CreateInstance(destinationType);
packedVec.PackFromVector4(value);
return packedVec;
}
return null;
}
示例3: Register
public AggregateDefinition Register(Type aggregateType)
{
AggregateDefinition definition = new AggregateDefinition();
definition.AggregateType = aggregateType;
var aggregateAttribute = ReflectionUtils.GetSingleAttribute<AggregateAttribute>(aggregateType);
definition.AggregateName = aggregateAttribute != null
? aggregateAttribute.AggregateName
: aggregateType.FullName;
var statefull = aggregateType.GetInterface(typeof(IStatefullAggregate).FullName);
if (statefull != null)
{
definition.AggregateKind = AggregateKind.Statefull;
if (aggregateType.BaseType == null
|| aggregateType.BaseType.IsGenericType == false
|| aggregateType.BaseType.GetGenericTypeDefinition() != typeof(StatefullAggregate<>))
throw new Exception(String.Format("We cannot find state type for [{0}] aggregate", aggregateType.FullName));
var genericArgs = aggregateType.BaseType.GetGenericArguments();
definition.StateType = genericArgs[0];
return definition;
}
var stateless = aggregateType.GetInterface(typeof(IStatelessAggregate).FullName);
if (stateless != null)
{
definition.AggregateKind = AggregateKind.Stateless;
return definition;
}
throw new Exception(String.Format("Object of type ({0}) not an aggregate", aggregateType));
}
示例4: CanConvertTo
/// <summary>
/// Overloaded. Returns whether this converter can convert the object to the specified type.
/// </summary>
/// <param name="context">An ITypeDescriptorContext that provides a format context.</param>
/// <param name="destinationType">A Type that represents the type you want to convert to.</param>
/// <returns>true if this converter can perform the conversion; otherwise, false.</returns>
public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType)
{
if (destinationType == null)
throw new ArgumentNullException("destinationType");
if( destinationType == typeof(ArrayCollection) )
return true;
if( destinationType.IsArray )
return true;
#if !SILVERLIGHT
if( destinationType == typeof(ArrayList) )
return true;
#endif
if( destinationType == typeof(IList) )
return true;
Type typeIList = destinationType.GetInterface("System.Collections.IList", false);
if(typeIList != null)
return true;
//generic interface
Type typeGenericICollection = destinationType.GetInterface("System.Collections.Generic.ICollection`1", false);
if (typeGenericICollection != null)
return true;
#if !SILVERLIGHT
return base.CanConvertTo(context, destinationType);
#else
return base.CanConvertTo(destinationType);
#endif
}
示例5: CollectionDescriptor
/// <summary>
/// Initializes a new instance of the <see cref="CollectionDescriptor" /> class.
/// </summary>
/// <param name="attributeRegistry">The attribute registry.</param>
/// <param name="type">The type.</param>
/// <param name="emitDefaultValues">if set to <c>true</c> [emit default values].</param>
/// <param name="namingConvention">The naming convention.</param>
/// <exception cref="System.ArgumentException">Expecting a type inheriting from System.Collections.ICollection;type</exception>
public CollectionDescriptor(IAttributeRegistry attributeRegistry, Type type, bool emitDefaultValues, IMemberNamingConvention namingConvention)
: base(attributeRegistry, type, emitDefaultValues, namingConvention)
{
if (!IsCollection(type))
throw new ArgumentException("Expecting a type inheriting from System.Collections.ICollection", "type");
// Gets the element type
var collectionType = type.GetInterface(typeof(IEnumerable<>));
ElementType = (collectionType != null) ? collectionType.GetGenericArguments()[0] : typeof(object);
// implements ICollection<T>
Type itype;
if ((itype = type.GetInterface(typeof(ICollection<>))) != null)
{
var add = itype.GetMethod("Add", new [] { ElementType });
CollectionAddFunction = (obj, value) => add.Invoke(obj, new [] { value });
var countMethod = itype.GetProperty("Count").GetGetMethod();
GetCollectionCountFunction = o => (int)countMethod.Invoke(o, null);
var isReadOnly = itype.GetProperty("IsReadOnly").GetGetMethod();
IsReadOnlyFunction = obj => (bool)isReadOnly.Invoke(obj, null);
isKeyedCollection = type.ExtendsGeneric(typeof (KeyedCollection<,>));
}
// implements IList
else if (typeof (IList).IsAssignableFrom(type))
{
CollectionAddFunction = (obj, value) => ((IList) obj).Add(value);
GetCollectionCountFunction = o => ((IList) o).Count;
IsReadOnlyFunction = obj => ((IList) obj).IsReadOnly;
}
}
示例6: CollectionDescriptor
/// <summary>
/// Initializes a new instance of the <see cref="CollectionDescriptor" /> class.
/// </summary>
/// <param name="factory">The factory.</param>
/// <param name="type">The type.</param>
/// <exception cref="System.ArgumentException">Expecting a type inheriting from System.Collections.ICollection;type</exception>
public CollectionDescriptor(ITypeDescriptorFactory factory, Type type) : base(factory, type)
{
if (!IsCollection(type))
throw new ArgumentException("Expecting a type inheriting from System.Collections.ICollection", "type");
// Gets the element type
var collectionType = type.GetInterface(typeof(IEnumerable<>));
ElementType = (collectionType != null) ? collectionType.GetGenericArguments()[0] : typeof(object);
Category = DescriptorCategory.Collection;
bool typeSupported = false;
// implements ICollection<T>
Type itype = type.GetInterface(typeof(ICollection<>));
if (itype != null)
{
var add = itype.GetMethod("Add", new[] {ElementType});
CollectionAddFunction = (obj, value) => add.Invoke(obj, new[] {value});
var clear = itype.GetMethod("Clear", Type.EmptyTypes);
CollectionClearFunction = obj => clear.Invoke(obj, EmptyObjects);
var countMethod = itype.GetProperty("Count").GetGetMethod();
GetCollectionCountFunction = o => (int)countMethod.Invoke(o, null);
var isReadOnly = itype.GetProperty("IsReadOnly").GetGetMethod();
IsReadOnlyFunction = obj => (bool)isReadOnly.Invoke(obj, null);
typeSupported = true;
}
// implements IList<T>
itype = type.GetInterface(typeof(IList<>));
if (itype != null)
{
var insert = itype.GetMethod("Insert", new[] { typeof(int), ElementType });
CollectionInsertFunction = (obj, index, value) => insert.Invoke(obj, new[] { index, value });
var removeAt = itype.GetMethod("RemoveAt", new[] { typeof(int) });
CollectionRemoveAtFunction = (obj, index) => removeAt.Invoke(obj, new object[] { index });
var getItem = itype.GetMethod("get_Item", new[] { typeof(int) });
var setItem = itype.GetMethod("set_Item", new[] { typeof(int), ElementType });
GetIndexedItem = (obj, index) => getItem.Invoke(obj, new object[] { index });
SetIndexedItem = (obj, index, value) => setItem.Invoke(obj, new[] { index, value });
hasIndexerAccessors = true;
}
// implements IList
if (!typeSupported && typeof(IList).IsAssignableFrom(type))
{
CollectionAddFunction = (obj, value) => ((IList)obj).Add(value);
CollectionClearFunction = obj => ((IList)obj).Clear();
CollectionInsertFunction = (obj, index, value) => ((IList)obj).Insert(index, value);
CollectionRemoveAtFunction = (obj, index) => ((IList)obj).RemoveAt(index);
GetCollectionCountFunction = o => ((IList)o).Count;
GetIndexedItem = (obj, index) => ((IList)obj)[index];
SetIndexedItem = (obj, index, value) => ((IList)obj)[index] = value;
IsReadOnlyFunction = obj => ((IList)obj).IsReadOnly;
hasIndexerAccessors = true;
typeSupported = true;
}
if (!typeSupported)
{
throw new ArgumentException("Type [{0}] is not supported as a modifiable collection".ToFormat(type), "type");
}
}
示例7: GetSerializer
public IBsonSerializer GetSerializer(Type type)
{
if (type == typeof(IGameObject) || type.IsSubclassOf(typeof(IGameObject)) || type.GetInterface(typeof(IGameObject).Name) != null)
return new GameObjectSerializer();
if (type == typeof(IDataObject) || type.IsSubclassOf(typeof(IDataObject)) || type.GetInterface(typeof(IDataObject).Name) != null)
return new DataObjectSerializer<IDataObject>();
return null;
}
示例8: Cast
public static Vertex Cast(Vertex vertexToCast, Type newVertexType)
{
Vertex ret = Activator.CreateInstance(newVertexType) as Vertex;
ret.Pos = vertexToCast.Pos;
if (newVertexType.GetInterface("ITextured") != null && vertexToCast is ITextured)
(ret as ITextured).TexCoord = (vertexToCast as ITextured).TexCoord;
if (newVertexType.GetInterface("INormal") != null && vertexToCast is INormal)
(ret as INormal).Normal = (vertexToCast as INormal).Normal;
if (newVertexType.GetInterface("ITex3") != null && vertexToCast is ITextured)
(ret as ITex3).TexCoord = (vertexToCast as ITex3).TexCoord;
return ret;
}
示例9: return
IEnumerable<ITypeAmendment> IAmendmentAttribute.GetAmendments(Type target)
{
// Class does not implement INotifyPropertyChanged. Implement it for the user.
if (target.GetCustomAttributes(typeof(NotifyPropertyChangedAttribute), true).Length > 0
&& target.GetInterface("System.ComponentModel.INotifyPropertyChanged") == null)
yield return (ITypeAmendment)typeof(NotificationAmendment<>).MakeGenericType(target).GetConstructor(Type.EmptyTypes).Invoke(new object[0]);
// Class implements INotifyPropertyChangedAmendment so that user can fire custom notificaitons
if (target.GetCustomAttributes(typeof(NotifyPropertyChangedAttribute), true).Length > 0
&& target.GetInterface("NotifyPropertyChanged.INotifyPropertyChangedAmendment") != null)
yield return (ITypeAmendment)typeof(SimpleNotificationAmendment<>).MakeGenericType(target).GetConstructor(Type.EmptyTypes).Invoke(new object[0]);
}
示例10: CacheableAttribute
/// <summary>
/// 利用指定的缓存策略提供程序创建 HtmlCacheableAttribute 对象。
/// </summary>
/// <param name="policyProviderType">缓存策略提供程序类型</param>
public CacheableAttribute( Type policyProviderType )
{
if ( policyProviderType.GetInterface( mvcCachePolicyProviderType.FullName ) == mvcCachePolicyProviderType )
{
CachePolicyProvider = Activator.CreateInstance( policyProviderType ).CastTo<IMvcCachePolicyProvider>();
return;
}
else if ( policyProviderType.GetInterface( cachePolicyProviderType.FullName ) == cachePolicyProviderType )
{
CachePolicyProvider = new MvcCachePolicyProviderWrapper( Activator.CreateInstance( policyProviderType ).CastTo<ICachePolicyProvider>() );
return;
}
throw new InvalidOperationException( "配置错误,类型必须从 IHtmlCachePolicyProvider 或 IMvcCachePolicyProvider 派生" );
}
示例11: AssemblyAnalyzerAttribute
public AssemblyAnalyzerAttribute (Type Type)
{
if (Type.GetInterface (typeof (IAnalyzer).FullName) == null)
throw new ArgumentException ("AssemblyAnalyzer can only point to types implementing IAnalyzer interface.");
type = Type;
}
示例12: addCommand
public void addCommand( string commandName, Type commandClass )
{
if ( commandClass == null )
{
Debug.LogError ( "Error in " + this + " Command can't be null." );
}
if ( commandClass.GetInterface ("IIwCommand") == null )
{
Debug.LogError ( "Error in " + this + " Command Class must be extends of IIwCommand interface." );
}
lock ( _commandList )
{
if ( _commandList.ContainsKey ( commandName ) )
{
_commandList[commandName].Add ( commandClass );
}
else
{
List<Type> commandListsByName = new List<Type> ();
commandListsByName.Add ( commandClass );
_commandList[commandName] = commandListsByName;
}
}
}
示例13: CanConvertTo
public static bool CanConvertTo(ITypeDescriptorContext context, Type destinationType)
{
if (destinationType == typeof(float))
{
return true;
}
if (destinationType == typeof(Vector2))
{
return true;
}
if (destinationType == typeof(Vector3))
{
return true;
}
if (destinationType == typeof(Vector4))
{
return true;
}
if (destinationType.GetInterface("IPackedVector") != null)
{
return true;
}
return false;
}
示例14: ValidateControllerType
public Res<Type, PluginControllerValidationResult> ValidateControllerType(Type controllerType)
{
var res = PluginControllerValidationResult.Success;
if (controllerType == null) throw new ArgumentNullException("Controller type must be supplied");
try
{
res = CChain<PluginControllerValidationResult>
// Must be a class
.If(() => !controllerType.IsClass, PluginControllerValidationResult.ControllerTypeNotAClass)
// Must be marshalable
.ThenIf(() => controllerType.BaseType == null || controllerType.BaseType != typeof(CrossAppDomainObject),
PluginControllerValidationResult.ControllerTypeNotMarshalable)
// Must implement the core controller interface
.ThenIf(() => controllerType.GetInterface(typeof(IPluginController).FullName) == null,
PluginControllerValidationResult.ControllerInterfaceNotImplemented)
// Must have a constructor taking an IKernel
//.ThenIf(() => controllerType.GetConstructor(new[] { typeof(IKernel) }) == null,
// PluginControllerValidationResult.KernelAcceptingConstructorNotFound)
.Result;
return new Res<Type, PluginControllerValidationResult>(res == PluginControllerValidationResult.Success,
controllerType,
res);
}
catch (Exception ex)
{
throw new ApplicationException("Failed to validate controller type", ex);
}
}
示例15: ActivateCodec
private static ICodec ActivateCodec(Type assemblyType)
{
if (assemblyType.GetInterface(typeof(ICodec).Name) == null)
return null;
return Activator.CreateInstance(assemblyType) as ICodec;
}