本文整理汇总了C#中System.Reflection.TypeInfo.IsAssignableFrom方法的典型用法代码示例。如果您正苦于以下问题:C# TypeInfo.IsAssignableFrom方法的具体用法?C# TypeInfo.IsAssignableFrom怎么用?C# TypeInfo.IsAssignableFrom使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Reflection.TypeInfo
的用法示例。
在下文中一共展示了TypeInfo.IsAssignableFrom方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: FindTypes
public IEnumerable<TypeInfo> FindTypes(IEnumerable<Assembly> targetAssmblies, TypeInfo targetTypeInfo)
{
var types = targetAssmblies
.SelectMany(GetLoadableTypes)
.Where(t => t.IsClass &&
!t.IsAbstract &&
!t.ContainsGenericParameters &&
targetTypeInfo.IsAssignableFrom(t));
return types;
}
示例2: ClearChanges
/// <summary>
/// Clears the change list for certain objects
/// </summary>
public void ClearChanges(GameObject targetObj, TypeInfo cmpType, PropertyInfo prop)
{
if (this.changes == null || this.changes.Count == 0) return;
IEnumerable<int> indexPath = targetObj != null ? this.obj.IndexPathOfChild(targetObj) : null;
for (int i = this.changes.Count - 1; i >= 0; i--)
{
if (indexPath != null && !this.changes[i].childIndex.SequenceEqual(indexPath)) continue;
if (cmpType != null && !cmpType.IsAssignableFrom(this.changes[i].componentType.GetTypeInfo())) continue;
if (prop != null && prop != this.changes[i].prop) continue;
this.changes.RemoveAt(i);
}
}
示例3: TryGetPartBuilder
/// <summary>
/// Tries to get the part builder.
/// </summary>
/// <param name="serviceContractMetadata">The service contract metadata.</param>
/// <param name="serviceContract">The service contract.</param>
/// <param name="conventions">The conventions.</param>
/// <param name="typeInfos">The type infos.</param>
/// <returns>
/// The part builder or <c>null</c>.
/// </returns>
private IPartConventionsBuilder TryGetPartBuilder(
AppServiceContractAttribute serviceContractMetadata,
TypeInfo serviceContract,
IConventionsBuilder conventions,
IEnumerable<TypeInfo> typeInfos)
{
var serviceContractType = serviceContract.AsType();
if (serviceContract.IsGenericTypeDefinition)
{
// if there is non-generic service contract with the same full name
// then add just the conventions for the derived types.
return conventions.ForTypesMatching(t => this.MatchOpenGenericContractType(t, serviceContractType));
}
if (serviceContractMetadata.AllowMultiple)
{
// if the service contract metadata allows multiple service registrations
// then add just the conventions for the derived types.
return conventions.ForTypesDerivedFrom(serviceContractType);
}
var parts =
typeInfos.Where(
ti =>
serviceContract.IsAssignableFrom(ti) && ti.IsClass && !ti.IsAbstract
&& ti.GetCustomAttribute<ExcludeFromCompositionAttribute>() == null).ToList();
if (parts.Count == 1)
{
return conventions.ForType(parts[0].AsType());
}
if (parts.Count > 1)
{
var overrideChain =
parts.ToDictionary(
ti => ti,
ti =>
ti.GetCustomAttribute<OverridePriorityAttribute>() ?? new OverridePriorityAttribute(Priority.Normal))
.OrderBy(item => item.Value.Value)
.ToList();
var selectedPart = overrideChain[0].Key;
if (overrideChain[0].Value.Value == overrideChain[1].Value.Value)
{
throw new InvalidOperationException(
string.Format(
Strings.AmbiguousOverrideForAppServiceContract,
serviceContract,
selectedPart,
string.Join(", ", overrideChain.Select(item => item.Key.ToString() + ":" + item.Value.Value))));
}
return conventions.ForType(selectedPart.AsType());
}
return null;
}
示例4: CheckInstanceExportCompatibility
private static void CheckInstanceExportCompatibility(TypeInfo partType, TypeInfo contractType)
{
if (partType.IsGenericTypeDefinition)
{
CheckGenericContractCompatibility(partType, partType, contractType);
}
else if (!contractType.IsAssignableFrom(partType))
{
var message = string.Format(Properties.Resources.TypeInspector_ContractNotAssignable, contractType.Name, partType.Name);
throw new CompositionFailedException(message);
}
}
示例5: MatchDerivedFromContractType
/// <summary>
/// Checks whether the part type matches the type of the open generic contract.
/// </summary>
/// <param name="partTypeInfo">Type of the part.</param>
/// <param name="serviceContract">Type of the service contract.</param>
/// <returns><c>true</c> if the part type matches the type of the generic contract, otherwise <c>false</c>.</returns>
private bool MatchDerivedFromContractType(TypeInfo partTypeInfo, TypeInfo serviceContract)
{
if (!this.IsEligiblePart(partTypeInfo) || partTypeInfo.IsGenericTypeDefinition)
{
return false;
}
return serviceContract.IsAssignableFrom(partTypeInfo);
}
示例6: MatchTypesByGenerics
private static IDictionary<Type, Type> MatchTypesByGenerics(Type[] types, TypeInfo genericTypeDefinition, TypeInfo baseTypeDefinition)
{
return types
.Where(t => t.GetTypeInfo().IsClass && baseTypeDefinition.IsAssignableFrom(t.GetTypeInfo())).ToArray()
.ToDictionary(x => genericTypeDefinition.MakeGenericType(x),
x =>
{
var genericTypeBase = genericTypeDefinition.MakeGenericType(x);
var result = types.Where(t => genericTypeBase.GetTypeInfo().IsAssignableFrom(t.GetTypeInfo())).ToArray();
if (result.Length > 1)
throw new AmbiguousMatchException($"Ambiguous match found for {x.FullName}: {String.Join(", ", result.Select(t => t.FullName))}");
return result.FirstOrDefault();
});
}
示例7: MatchTypes
private static IDictionary<Type, Type> MatchTypes(Type[] types, TypeInfo baseTypeDefinition)
{
return types
.Where(t => t.GetTypeInfo().IsInterface && baseTypeDefinition.IsAssignableFrom(t.GetTypeInfo())).ToArray()
.ToDictionary(x => x,
x =>
{
var result = types.Where(t => (t != x) && x.GetTypeInfo().IsAssignableFrom(t.GetTypeInfo())).ToArray();
if (result.Length > 1)
throw new AmbiguousMatchException($"Ambiguous match found for {x.FullName}: {String.Join(", ", result.Select(t => t.FullName))}");
return result.FirstOrDefault();
});
}