本文整理汇总了C#中System.Reflection.TypeInfo类的典型用法代码示例。如果您正苦于以下问题:C# TypeInfo类的具体用法?C# TypeInfo怎么用?C# TypeInfo使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
TypeInfo类属于System.Reflection命名空间,在下文中一共展示了TypeInfo类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: GetConstructors
public static ConstructorInfo[] GetConstructors(TypeInfo typeInfo, bool nonPublic = false)
{
if (nonPublic)
return typeInfo.DeclaredConstructors.ToArray();
return
typeInfo.DeclaredConstructors.Where(x => x.IsPublic).ToArray();
}
示例2: GetComponentFullName
public static string GetComponentFullName(TypeInfo componentType)
{
if (componentType == null)
{
throw new ArgumentNullException(nameof(componentType));
}
var attribute = componentType.GetCustomAttribute<ViewComponentAttribute>();
if (!string.IsNullOrEmpty(attribute?.Name))
{
return attribute.Name;
}
// If the view component didn't define a name explicitly then use the namespace + the
// 'short name'.
var shortName = GetShortNameByConvention(componentType);
if (string.IsNullOrEmpty(componentType.Namespace))
{
return shortName;
}
else
{
return componentType.Namespace + "." + shortName;
}
}
示例3: IsValid
private static bool IsValid(Type type, TypeInfo typeInfo)
{
if (!ReferenceEquals(null, type))
{
if (typeInfo.IsArray)
{
type = type.GetElementType();
}
if (typeInfo.IsAnonymousType || type.IsAnonymousType())
{
var properties = type.GetProperties().Select(x => x.Name).ToList();
var propertyNames = typeInfo.Properties.Select(x => x.Name).ToList();
var match =
type.IsAnonymousType() &&
typeInfo.IsAnonymousType &&
properties.Count == propertyNames.Count &&
propertyNames.All(x => properties.Contains(x));
if (!match)
{
return false;
}
}
return true;
}
return false;
}
示例4: FormatNonGenericTypeName
private static string FormatNonGenericTypeName(TypeInfo typeInfo, CommonTypeNameFormatterOptions options)
{
if (options.ShowNamespaces)
{
return typeInfo.FullName.Replace('+', '.');
}
if (typeInfo.DeclaringType == null)
{
return typeInfo.Name;
}
var stack = ArrayBuilder<string>.GetInstance();
do
{
stack.Push(typeInfo.Name);
typeInfo = typeInfo.DeclaringType?.GetTypeInfo();
} while (typeInfo != null);
stack.ReverseContents();
var typeName = string.Join(".", stack);
stack.Free();
return typeName;
}
示例5: FindSyncMethod
public static MethodInfo FindSyncMethod(TypeInfo componentType, object[] args)
{
if (componentType == null)
{
throw new ArgumentNullException(nameof(componentType));
}
var method = GetMethod(componentType, args, SyncMethodName);
if (method == null)
{
return null;
}
if (method.ReturnType == typeof(void))
{
throw new InvalidOperationException(
Resources.FormatViewComponent_SyncMethod_ShouldReturnValue(SyncMethodName));
}
else if (method.ReturnType.IsAssignableFrom(typeof(Task)))
{
throw new InvalidOperationException(
Resources.FormatViewComponent_SyncMethod_CannotReturnTask(SyncMethodName, nameof(Task)));
}
return method;
}
示例6: CloseGenericExport
public override DiscoveredExport CloseGenericExport(TypeInfo closedPartType, Type[] genericArguments)
{
var closedContractType = Contract.ContractType.MakeGenericType(genericArguments);
var newContract = Contract.ChangeType(closedContractType);
var property = closedPartType.AsType().GetRuntimeProperty(_property.Name);
return new DiscoveredPropertyExport(newContract, Metadata, property);
}
示例7: InheritsOrImplements
public static bool InheritsOrImplements(this TypeInfo child, TypeInfo parent)
{
if (child == null || parent == null)
return false;
parent = resolveGenericTypeDefinition(parent);
var currentChild = child.IsGenericType
? child.GetGenericTypeDefinition().GetTypeInfo()
: child;
while (currentChild != typeof(object).GetTypeInfo())
{
if (parent == currentChild || hasAnyInterfaces(parent, currentChild))
return true;
currentChild = currentChild.BaseType != null
&& currentChild.BaseType.GetTypeInfo().IsGenericType
? currentChild.BaseType.GetTypeInfo().GetGenericTypeDefinition().GetTypeInfo()
: currentChild.BaseType.GetTypeInfo();
if (currentChild == null)
return false;
}
return false;
}
示例8: FindSyncMethod
/// <summary>
/// Finds a synchronous method to execute.
/// </summary>
/// <param name="context">The widget context.</param>
/// <param name="widgetType">The widget type.</param>
/// <returns>The synchronous method.</returns>
public static MethodInfo FindSyncMethod(WidgetContext context, TypeInfo widgetType)
{
string httpMethod = ResolveHttpMethod(context);
string state = string.Empty; // Resolve a widget state?
MethodInfo method = null;
for (int i = 0; i < SyncMethodNames.Length; i++)
{
string name = string.Format(SyncMethodNames[i], state, httpMethod);
method = GetMethod(name, widgetType);
if (method != null)
{
break;
}
}
if (method == null)
{
return null;
}
if (method.ReturnType == typeof(void))
{
throw new InvalidOperationException($"Sync method '{method.Name}' should return a value.");
}
if (method.ReturnType.IsAssignableFrom(typeof(Task)))
{
throw new InvalidOperationException($"Sync method '{method.Name}' cannot return a task.");
}
return method;
}
示例9: DiscoverPropertyExports
private IEnumerable<DiscoveredExport> DiscoverPropertyExports(TypeInfo partType)
{
var partTypeAsType = partType.AsType();
foreach (var property in partTypeAsType.GetRuntimeProperties()
.Where(pi => pi.CanRead && pi.GetMethod.IsPublic && !pi.GetMethod.IsStatic))
{
foreach (var export in _attributeContext.GetDeclaredAttributes<ExportAttribute>(partTypeAsType, property))
{
IDictionary<string, object> metadata = new Dictionary<string, object>();
ReadMetadataAttribute(export, metadata);
var applied = _attributeContext.GetDeclaredAttributes(partTypeAsType, property);
ReadLooseMetadata(applied, metadata);
var contractType = export.ContractType ?? property.PropertyType;
CheckPropertyExportCompatibility(partType, property, contractType.GetTypeInfo());
var exportKey = new CompositionContract(export.ContractType ?? property.PropertyType, export.ContractName);
if (metadata.Count == 0)
metadata = s_noMetadata;
yield return new DiscoveredPropertyExport(exportKey, metadata, property);
}
}
}
示例10: CreateMethod
private static TestMethod CreateMethod(TypeInfo type, object instance, MethodInfo method)
{
TestMethod test = new TestMethod();
test.Name = method.Name;
if (method.GetCustomAttribute<AsyncTestMethodAttribute>(true) != null)
{
test.Test = new AsyncTestMethodAsyncAction(instance, method);
}
else
{
test.Test = new TestMethodAsyncAction(instance, method);
}
ExcludeTestAttribute excluded = method.GetCustomAttribute<ExcludeTestAttribute>(true);
if (excluded != null)
{
test.Exclude(excluded.Reason);
}
if (method.GetCustomAttribute<FunctionalTestAttribute>(true) != null)
{
test.Tags.Add("Functional");
}
test.Tags.Add(type.FullName + "." + method.Name);
test.Tags.Add(type.Name + "." + method.Name);
foreach (TagAttribute attr in method.GetCustomAttributes<TagAttribute>(true))
{
test.Tags.Add(attr.Tag);
}
return test;
}
示例11: FindAsyncMethod
/// <summary>
/// Finds an asynchronous method to execute.
/// </summary>
/// <param name="context">The widget context.</param>
/// <param name="widgetType">The widget type.</param>
/// <returns>The asynchronous method.</returns>
public static MethodInfo FindAsyncMethod(WidgetContext context, TypeInfo widgetType)
{
string httpMethod = ResolveHttpMethod(context);
string state = string.Empty; // Resolve a widget state?
MethodInfo method = null;
for (int i = 0; i < AsyncMethodNames.Length; i++)
{
string name = string.Format(AsyncMethodNames[i], state, httpMethod);
method = GetMethod(name, widgetType);
if (method != null)
{
break;
}
}
if (method == null)
{
return null;
}
if (!method.ReturnType.GetTypeInfo().IsGenericType
|| method.ReturnType.GetGenericTypeDefinition() != typeof(Task<>))
{
throw new InvalidOperationException($"Async method '{method.Name}' must return a task.");
}
return method;
}
示例12: HandlerDescriptor
public HandlerDescriptor(TypeInfo handlerType, DispatchingPriority priority)
{
Argument.IsNotNull(handlerType, nameof(handlerType));
HandlerType = handlerType;
Priority = priority;
}
示例13: IsSupportedPrimitive
protected static bool IsSupportedPrimitive(TypeInfo typeInfo)
{
return typeInfo.IsPrimitive
|| typeInfo.IsEnum
|| typeInfo == typeof(string).GetTypeInfo()
|| IsNullable(typeInfo.AsType());
}
示例14: ControllerModel
public ControllerModel(
TypeInfo controllerType,
IReadOnlyList<object> attributes)
{
if (controllerType == null)
{
throw new ArgumentNullException(nameof(controllerType));
}
if (attributes == null)
{
throw new ArgumentNullException(nameof(attributes));
}
ControllerType = controllerType;
Actions = new List<ActionModel>();
ApiExplorer = new ApiExplorerModel();
Attributes = new List<object>(attributes);
ControllerProperties = new List<PropertyModel>();
Filters = new List<IFilterMetadata>();
Properties = new Dictionary<object, object>();
RouteValues = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);
Selectors = new List<SelectorModel>();
}
示例15: BuildTagHelperDescriptors
private static IEnumerable<TagHelperDescriptor> BuildTagHelperDescriptors(
TypeInfo typeInfo,
string assemblyName,
IEnumerable<TagHelperAttributeDescriptor> attributeDescriptors,
IEnumerable<TargetElementAttribute> targetElementAttributes)
{
var typeName = typeInfo.FullName;
// If there isn't an attribute specifying the tag name derive it from the name
if (!targetElementAttributes.Any())
{
var name = typeInfo.Name;
if (name.EndsWith(TagHelperNameEnding, StringComparison.OrdinalIgnoreCase))
{
name = name.Substring(0, name.Length - TagHelperNameEnding.Length);
}
return new[]
{
BuildTagHelperDescriptor(
ToHtmlCase(name),
typeName,
assemblyName,
attributeDescriptors,
requiredAttributes: Enumerable.Empty<string>())
};
}
return targetElementAttributes.Select(
attribute => BuildTagHelperDescriptor(typeName, assemblyName, attributeDescriptors, attribute));
}