本文整理汇总了C#中System.IsAssignableFrom方法的典型用法代码示例。如果您正苦于以下问题:C# System.IsAssignableFrom方法的具体用法?C# System.IsAssignableFrom怎么用?C# System.IsAssignableFrom使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System
的用法示例。
在下文中一共展示了System.IsAssignableFrom方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: FindType
public static System.Type FindType(string typeName, System.Type baseType, bool useFullName = false, bool ignoreCase = false)
{
if (string.IsNullOrEmpty(typeName)) return null;
if (baseType == null) throw new System.ArgumentNullException("baseType");
StringComparison e = (ignoreCase) ? StringComparison.OrdinalIgnoreCase : StringComparison.Ordinal;
if(useFullName)
{
foreach (var assemb in System.AppDomain.CurrentDomain.GetAssemblies())
{
foreach (var t in assemb.GetTypes())
{
if (baseType.IsAssignableFrom(t) && string.Equals(t.FullName, typeName, e)) return t;
}
}
}
else
{
foreach (var assemb in System.AppDomain.CurrentDomain.GetAssemblies())
{
foreach (var t in assemb.GetTypes())
{
if (baseType.IsAssignableFrom(t) && (string.Equals(t.Name, typeName, e) || string.Equals(t.FullName, typeName, e))) return t;
}
}
}
return null;
}
示例2: GetTypesAssignableFrom
public static IEnumerable<Type> GetTypesAssignableFrom(System.Reflection.Assembly assemb, System.Type rootType)
{
foreach (var tp in assemb.GetTypes())
{
if (rootType.IsAssignableFrom(tp) && rootType != tp) yield return tp;
}
}
示例3: ComponentField
public static Component ComponentField(Rect position, GUIContent label, Component value, System.Type inheritsFromType, bool allowSceneObjects, System.Type targetComponentType)
{
if (inheritsFromType == null) inheritsFromType = typeof(Component);
else if (!typeof(Component).IsAssignableFrom(inheritsFromType) && !typeof(IComponent).IsAssignableFrom(inheritsFromType)) throw new TypeArgumentMismatchException(inheritsFromType, typeof(IComponent), "Type must inherit from IComponent or Component.", "inheritsFromType");
if (targetComponentType == null) throw new System.ArgumentNullException("targetComponentType");
if (!typeof(Component).IsAssignableFrom(targetComponentType)) throw new TypeArgumentMismatchException(targetComponentType, typeof(Component), "targetComponentType");
if (value != null && !targetComponentType.IsAssignableFrom(value.GetType())) throw new TypeArgumentMismatchException(value.GetType(), inheritsFromType, "value must inherit from " + inheritsFromType.Name, "value");
if (TypeUtil.IsType(inheritsFromType, typeof(Component)))
{
return EditorGUI.ObjectField(position, label, value, inheritsFromType, true) as Component;
}
else
{
value = EditorGUI.ObjectField(position, label, value, typeof(Component), true) as Component;
var go = GameObjectUtil.GetGameObjectFromSource(value);
if (go != null)
{
foreach (var c in go.GetComponents(inheritsFromType))
{
if (TypeUtil.IsType(c.GetType(), targetComponentType))
{
return c as Component;
}
}
}
}
return null;
}
示例4: PickRegistration
public override object PickRegistration(System.Type type, string identifier)
{
if (type.IsInterface) {
Type[] implementingTypes = AppDomain.CurrentDomain.GetAssemblies().ToList().SelectMany(s => s.GetTypes()).Where(prop => !prop.Equals(type) && type.IsAssignableFrom(prop)).ToArray();
if (implementingTypes.Count() == 1) {
return implementingTypes.First();
}
}
if ((this.NextStep != null)) {
return this.NextStep.PickRegistration(type, identifier);
}
return null;
}
示例5: Map
override public void Map(System.Type type, System.Type mapper, object instance)
{
if ((instance is MonoBehaviour) == false)
base.Map(type, mapper, instance);
else
{
DesignByContract.Check.Require(instance != null);
DesignByContract.Check.Require(type.IsAssignableFrom(instance.GetType()), "Trying to register an invalid instance");
KeyValuePair<WeakReference, bool> valuePair = new KeyValuePair<WeakReference, bool>(new WeakReference(instance), false);
_mbcache[type] = valuePair;
}
}
示例6: ClassList
private static IEnumerable<System.Type> ClassList(Assembly assembly, System.Type type)
{
IList<System.Type> result = new List<System.Type>();
if (assembly != null)
{
System.Type[] types = assembly.GetTypes();
foreach (System.Type tp in types)
{
if (tp != type && type.IsAssignableFrom(tp) && !tp.IsInterface)
result.Add(tp);
}
}
return result;
}
示例7: FindSubclassesOf
public static IEnumerable<System.Type> FindSubclassesOf(System.Type t)
{
var subclasses = new List<System.Type>();
foreach(var asm in System.AppDomain.CurrentDomain.GetAssemblies())
{
foreach (var type in asm.GetTypes())
{
if (type.IsAbstract == false && t.IsAssignableFrom(type))
{
subclasses.Add(type);
}
}
}
return subclasses;
}
示例8: ResourceAssociationSetEnd
public ResourceAssociationSetEnd(System.Data.Services.Providers.ResourceSet resourceSet, System.Data.Services.Providers.ResourceType resourceType, System.Data.Services.Providers.ResourceProperty resourceProperty)
{
WebUtil.CheckArgumentNull<System.Data.Services.Providers.ResourceSet>(resourceSet, "resourceSet");
WebUtil.CheckArgumentNull<System.Data.Services.Providers.ResourceType>(resourceType, "resourceType");
if ((resourceProperty != null) && ((resourceType.TryResolvePropertyName(resourceProperty.Name) == null) || (resourceProperty.TypeKind != ResourceTypeKind.EntityType)))
{
throw new ArgumentException(Strings.ResourceAssociationSetEnd_ResourcePropertyMustBeNavigationPropertyOnResourceType);
}
if (!resourceSet.ResourceType.IsAssignableFrom(resourceType) && !resourceType.IsAssignableFrom(resourceSet.ResourceType))
{
throw new ArgumentException(Strings.ResourceAssociationSetEnd_ResourceTypeMustBeAssignableToResourceSet);
}
if ((resourceProperty != null) && (DataServiceProviderWrapper.GetDeclaringTypeForProperty(resourceType, resourceProperty, null) != resourceType))
{
throw new ArgumentException(Strings.ResourceAssociationSetEnd_ResourceTypeMustBeTheDeclaringType(resourceType.FullName, resourceProperty.Name));
}
this.resourceSet = resourceSet;
this.resourceType = resourceType;
this.resourceProperty = resourceProperty;
}
示例9: FormatObjectInternal
private static object FormatObjectInternal(object value, System.Type targetType, TypeConverter sourceConverter, TypeConverter targetConverter, string formatString, IFormatProvider formatInfo, object formattedNullValue)
{
if ((value == DBNull.Value) || (value == null))
{
if (formattedNullValue != null)
{
return formattedNullValue;
}
if (targetType == stringType)
{
return string.Empty;
}
if (targetType == checkStateType)
{
return CheckState.Indeterminate;
}
return null;
}
if (((targetType == stringType) && (value is IFormattable)) && !string.IsNullOrEmpty(formatString))
{
return (value as IFormattable).ToString(formatString, formatInfo);
}
System.Type type = value.GetType();
TypeConverter converter = TypeDescriptor.GetConverter(type);
if (((sourceConverter != null) && (sourceConverter != converter)) && sourceConverter.CanConvertTo(targetType))
{
return sourceConverter.ConvertTo(null, GetFormatterCulture(formatInfo), value, targetType);
}
TypeConverter converter2 = TypeDescriptor.GetConverter(targetType);
if (((targetConverter != null) && (targetConverter != converter2)) && targetConverter.CanConvertFrom(type))
{
return targetConverter.ConvertFrom(null, GetFormatterCulture(formatInfo), value);
}
if (targetType == checkStateType)
{
if (type == booleanType)
{
return (((bool) value) ? CheckState.Checked : CheckState.Unchecked);
}
if (sourceConverter == null)
{
sourceConverter = converter;
}
if ((sourceConverter != null) && sourceConverter.CanConvertTo(booleanType))
{
return (((bool) sourceConverter.ConvertTo(null, GetFormatterCulture(formatInfo), value, booleanType)) ? CheckState.Checked : CheckState.Unchecked);
}
}
if (targetType.IsAssignableFrom(type))
{
return value;
}
if (sourceConverter == null)
{
sourceConverter = converter;
}
if (targetConverter == null)
{
targetConverter = converter2;
}
if ((sourceConverter != null) && sourceConverter.CanConvertTo(targetType))
{
return sourceConverter.ConvertTo(null, GetFormatterCulture(formatInfo), value, targetType);
}
if ((targetConverter != null) && targetConverter.CanConvertFrom(type))
{
return targetConverter.ConvertFrom(null, GetFormatterCulture(formatInfo), value);
}
if (!(value is IConvertible))
{
throw new FormatException(GetCantConvertMessage(value, targetType));
}
return ChangeType(value, targetType, formatInfo);
}
示例10: CanCastTo
/// <summary>
/// Checks whether the proxy representing the specified object type can be cast to the type represented by the IRemotingTypeInfo interface
/// </summary>
/// <param name="toType">The Type we wish to cast to</param>
/// <param name="obj">The object we wish to cast</param>
/// <returns>True if the strict property is false, otherwise the list of supportedTypes is checked.<br>
/// The proxy targets type(s) are automatically supported</br></returns>
public bool CanCastTo(System.Type toType, object obj)
{
// Assume we can (which is the default unless strict is true)
bool canCast = true;
if (strict) {
// First check if the proxyTarget supports the cast
if (toType.IsAssignableFrom(proxyTarget.GetType())) {
canCast = true;
} else if (supportedTypes != null) {
canCast = false;
// Check if the list of supported interfaces supports the cast
foreach(Type type in supportedTypes) {
if (toType == type) {
canCast = true;
break;
}
}
} else {
canCast = false;
}
}
return canCast;
}
示例11: GetImplementorClasses
/// <summary>
/// Added to solve a problem with SessionImpl.Find( CriteriaImpl ),
/// see the comment there for an explanation.
/// </summary>
/// <param name="clazz"></param>
/// <returns></returns>
public System.Type[] GetImplementorClasses(System.Type clazz)
{
ArrayList results = new ArrayList();
foreach (IEntityPersister p in classPersisters.Values)
{
if (p is IQueryable)
{
IQueryable q = (IQueryable) p;
bool isMappedClass = clazz.Equals(q.MappedClass);
if (q.IsExplicitPolymorphism)
{
if (isMappedClass)
{
return new System.Type[] {q.MappedClass};
}
}
else
{
if (isMappedClass)
{
results.Add(q.MappedClass);
}
else if (
clazz.IsAssignableFrom(q.MappedClass) &&
(!q.IsInherited || !clazz.IsAssignableFrom(q.MappedSuperclass)))
{
results.Add(q.MappedClass);
}
}
}
}
return (System.Type[]) results.ToArray(typeof(System.Type));
}
示例12: ParseObjectInternal
private static object ParseObjectInternal(object value, System.Type targetType, System.Type sourceType, TypeConverter targetConverter, TypeConverter sourceConverter, IFormatProvider formatInfo, object formattedNullValue)
{
if (EqualsFormattedNullValue(value, formattedNullValue, formatInfo) || (value == DBNull.Value))
{
return DBNull.Value;
}
TypeConverter converter = TypeDescriptor.GetConverter(targetType);
if (((targetConverter != null) && (converter != targetConverter)) && targetConverter.CanConvertFrom(sourceType))
{
return targetConverter.ConvertFrom(null, GetFormatterCulture(formatInfo), value);
}
TypeConverter converter2 = TypeDescriptor.GetConverter(sourceType);
if (((sourceConverter != null) && (converter2 != sourceConverter)) && sourceConverter.CanConvertTo(targetType))
{
return sourceConverter.ConvertTo(null, GetFormatterCulture(formatInfo), value, targetType);
}
if (value is string)
{
object obj2 = InvokeStringParseMethod(value, targetType, formatInfo);
if (obj2 != parseMethodNotFound)
{
return obj2;
}
}
else if (value is CheckState)
{
CheckState state = (CheckState) value;
if (state == CheckState.Indeterminate)
{
return DBNull.Value;
}
if (targetType == booleanType)
{
return (state == CheckState.Checked);
}
if (targetConverter == null)
{
targetConverter = converter;
}
if ((targetConverter != null) && targetConverter.CanConvertFrom(booleanType))
{
return targetConverter.ConvertFrom(null, GetFormatterCulture(formatInfo), state == CheckState.Checked);
}
}
else if ((value != null) && targetType.IsAssignableFrom(value.GetType()))
{
return value;
}
if (targetConverter == null)
{
targetConverter = converter;
}
if (sourceConverter == null)
{
sourceConverter = converter2;
}
if ((targetConverter != null) && targetConverter.CanConvertFrom(sourceType))
{
return targetConverter.ConvertFrom(null, GetFormatterCulture(formatInfo), value);
}
if ((sourceConverter != null) && sourceConverter.CanConvertTo(targetType))
{
return sourceConverter.ConvertTo(null, GetFormatterCulture(formatInfo), value, targetType);
}
if (!(value is IConvertible))
{
throw new FormatException(GetCantConvertMessage(value, targetType));
}
return ChangeType(value, targetType, formatInfo);
}
示例13: ValidateCurrentBehaviourClass
private void ValidateCurrentBehaviourClass(System.Type myType)
{
if (myTarget.Behaviour == null)
{
object o = CreateInstance(myType);
myTarget.Behaviour = (A_InteractBehaviour)o;
}
else
{
// If Different Behaviour Type
if ( !(myType.IsAssignableFrom( myTarget.Behaviour.GetType() ) ) )
{
// Creates New Instance Of Type And Assigns It
object o = CreateInstance(myType);
myTarget.Behaviour = (A_InteractBehaviour)o;
}
}
}
示例14: SetClass
/// <summary>Sets the value of the <code>name</code> property to the name of a class.
/// First checks that the class implements the named interface.
/// </summary>
public virtual void SetClass(System.String propertyName, System.Type theClass, System.Type xface)
{
if (!xface.IsAssignableFrom(theClass))
{
throw new System.SystemException(theClass + " not " + xface.FullName);
}
SetProperty(propertyName, theClass.FullName);
}
示例15: ConvertType
private static object ConvertType(object value, System.Type type)
{
if (value == null)
return null;
if (type.IsAssignableFrom(value.GetType()))
return value;
if (IsNullableOfT(type))
type = Nullable.GetUnderlyingType(type);
if (type.IsEnum)
return Enum.ToObject(type, value);
if (type.IsPrimitive)
return Convert.ChangeType(value, type);
throw new Exception("Cannot convert '" + value.ToString() + "' to " + type.ToString());
}