本文整理汇总了C#中System.Reflection.TypeInfo.AsType方法的典型用法代码示例。如果您正苦于以下问题:C# TypeInfo.AsType方法的具体用法?C# TypeInfo.AsType怎么用?C# TypeInfo.AsType使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Reflection.TypeInfo
的用法示例。
在下文中一共展示了TypeInfo.AsType方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: RewriteActivator
public override CompositeActivator RewriteActivator(
TypeInfo partType,
CompositeActivator activator,
IDictionary<string, object> partMetadata,
IEnumerable<CompositionDependency> dependencies)
{
var propertyDependencies = dependencies
.Where(dep => dep.Site is PropertyImportSite)
.ToDictionary(d => ((PropertyImportSite)d.Site).Property);
if (propertyDependencies.Count == 0)
return activator;
var lc = Expression.Parameter(typeof(LifetimeContext));
var op = Expression.Parameter(typeof(CompositionOperation));
var inst = Expression.Parameter(typeof(object));
var typed = Expression.Variable(partType.AsType());
var statements = new List<Expression>();
var assignTyped = Expression.Assign(typed, Expression.Convert(inst, partType.AsType()));
statements.Add(assignTyped);
foreach (var d in propertyDependencies)
{
var property = d.Key;
var assignment = Expression.Assign(
Expression.MakeMemberAccess(typed, property),
Expression.Convert(
Expression.Call(
Expression.Constant(d.Value.Target.GetDescriptor().Activator),
s_activatorInvokeMethod,
lc,
op),
property.PropertyType));
statements.Add(assignment);
}
statements.Add(inst);
var setAll = Expression.Block(new[] { typed }, statements);
var setAction = Expression.Lambda<Func<object, LifetimeContext, CompositionOperation, object>>(
setAll, inst, lc, op).Compile();
return (c, o) =>
{
var i = activator(c, o);
o.AddNonPrerequisiteAction(() => setAction(i, c, o));
return i;
};
}
示例2: 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);
}
}
}
示例3: GetMethod
private static MethodInfo GetMethod(TypeInfo componentType, object[] args, string methodName)
{
args = args ?? new object[0];
var argumentExpressions = new Expression[args.Length];
for (var i = 0; i < args.Length; i++)
{
argumentExpressions[i] = Expression.Constant(args[i], args[i].GetType());
}
try
{
// We're currently using this technique to make a call into a component method that looks like a
// regular method call.
//
// Ex: @Component.Invoke<Cart>("hello", 5) => cart.Invoke("hello", 5)
//
// This approach has some drawbacks, namely it doesn't account for default parameters, and more
// noticably, it throws if the method is not found.
//
// Unfortunely the overload of Type.GetMethod that we would like to use is not present in CoreCLR.
// Item #160 in Jira tracks these issues.
var expression = Expression.Call(
Expression.Constant(null, componentType.AsType()),
methodName,
null,
argumentExpressions);
return expression.Method;
}
catch (InvalidOperationException)
{
return null;
}
}
示例4: 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);
}
示例5: GetMethod
private static MethodInfo GetMethod(TypeInfo componentType, object[] args, string methodName)
{
Type[] types;
if (args == null || args.Length == 0)
{
types = Type.EmptyTypes;
}
else
{
types = new Type[args.Length];
for (var i = 0; i < args.Length; i++)
{
types[i] = args[i]?.GetType() ?? typeof(object);
}
}
#if NET451
return componentType.AsType().GetMethod(
methodName,
BindingFlags.Public | BindingFlags.Instance,
binder: null,
types: types,
modifiers: null);
#else
var method = componentType.AsType().GetMethod(methodName, types: types);
// At most one method (including static and instance methods) with the same parameter types can exist
// per type.
return method != null && method.IsStatic ? null : method;
#endif
}
示例6: IsSupportedPrimitive
protected static bool IsSupportedPrimitive(TypeInfo typeInfo)
{
return typeInfo.IsPrimitive
|| typeInfo.IsEnum
|| typeInfo == typeof(string).GetTypeInfo()
|| IsNullable(typeInfo.AsType());
}
示例7: GetTypeFromInfo
public static System.Type GetTypeFromInfo ( TP type ) {
#if NETFX_CORE
return type.AsType();
#else
return type;
#endif
}
示例8: GetDependencies
public override IEnumerable<CompositionDependency> GetDependencies(TypeInfo partType, DependencyAccessor definitionAccessor)
{
var partTypeAsType = partType.AsType();
var imports = (from pi in partTypeAsType.GetRuntimeProperties()
.Where(pi => pi.CanWrite && pi.SetMethod.IsPublic && !(pi.SetMethod.IsStatic))
let attrs = _attributeContext.GetDeclaredAttributes(pi.DeclaringType, pi).ToArray()
let site = new PropertyImportSite(pi)
where attrs.Any(a => a is ImportAttribute || a is ImportManyAttribute)
select new { Site = site, ImportInfo = ContractHelpers.GetImportInfo(pi.PropertyType, attrs, site) }).ToArray();
if (imports.Length == 0)
return NoDependencies;
var result = new List<CompositionDependency>();
foreach (var i in imports)
{
if (!i.ImportInfo.AllowDefault)
{
result.Add(definitionAccessor.ResolveRequiredDependency(i.Site, i.ImportInfo.Contract, false));
}
else
{
CompositionDependency optional;
if (definitionAccessor.TryResolveOptionalDependency(i.Site, i.ImportInfo.Contract, false, out optional))
result.Add(optional);
// Variation from CompositionContainer behaviour: we don't have to support recomposition
// so we don't require that defaultable imports be set to null.
}
}
return result;
}
示例9: ProcessType
private void ProcessType(TypeInfo typeInfo)
{
var type = typeInfo.AsType();
if (alreadyProcessed.Contains(type) || typeInfo.IsInterface || typeInfo.IsAbstract || !condition(type)) return;
alreadyProcessed.Add(type);
callback(type);
}
示例10: CreateMigration
public virtual Migration CreateMigration(TypeInfo migrationClass, string activeProvider)
{
Check.NotNull(activeProvider, nameof(activeProvider));
var migration = (Migration)Activator.CreateInstance(migrationClass.AsType());
migration.ActiveProvider = activeProvider;
return migration;
}
示例11: Build
private HarshContentTypeId Build(TypeInfo t)
{
if (t.AsType() == typeof(HarshEntity))
{
// don't recurse up to Object. we should never get
// here anyway, since entities are supposed to inherit from
// something with an absolute ID specified,
// not directly from the HarshEntity class.
return null;
}
var cta = t.GetCustomAttribute<ContentTypeAttribute>(inherit: false);
if (cta == null)
{
if (t == _entityTypeInfo)
{
throw Logger.Fatal.InvalidOperationFormat(
SR.ContentTypeIdBuilder_NoContentTypeAttribute,
t.FullName
);
}
else
{
throw Logger.Fatal.InvalidOperationFormat(
SR.ContentTypeIdBuilder_NoContentTypeAttributeBaseClass,
t.FullName,
_entityTypeInfo.FullName
);
}
}
var ctid = HarshContentTypeId.Parse(cta.ContentTypeId);
if (ctid.IsAbsolute)
{
// an absolute ID. do not recurse further up the
// class hierarchy, take it as it is
return ctid;
}
else
{
// not an absolute ID, append the parent type ID first
var result = Build(t.BaseType.GetTypeInfo());
if (result == null)
{
throw Logger.Fatal.InvalidOperationFormat(
SR.ContentTypeIdBuilder_NoAbsoluteIDInHierarchy,
_entityTypeInfo.FullName
);
}
return result.Append(ctid);
}
}
示例12: GetUnderlyingType
/// <summary>
/// Gets the underlying element type.
/// </summary>
/// <param name="type">The type to extract the underlying type (if any).</param>
/// <returns>The underlying element type.</returns>
/// <returns>The underlying type.</returns>
public static Type GetUnderlyingType(TypeInfo type)
{
if (type.IsArray)
{
return type.GetElementType();
}
if (type.IsGenericType == false)
{
return type.AsType();
}
Type enumerableType;
if (TryGetEnumerableType(type, out enumerableType))
{
return enumerableType.GetTypeInfo().GenericTypeArguments[0]; ;
}
return type.AsType();
}
示例13: CreateCandidate
private static ViewComponentDescriptor CreateCandidate(TypeInfo typeInfo)
{
var candidate = new ViewComponentDescriptor()
{
FullName = ViewComponentConventions.GetComponentFullName(typeInfo),
ShortName = ViewComponentConventions.GetComponentName(typeInfo),
Type = typeInfo.AsType(),
};
return candidate;
}
示例14: CreateDescriptor
/// <summary>
/// Creates a descriptor for the given widget type.
/// </summary>
/// <param name="typeInfo">The widget type.</param>
/// <returns>The widget descriptor.</returns>
private static WidgetDescriptor CreateDescriptor(TypeInfo typeInfo)
{
var descriptor = new WidgetDescriptor
{
FullName = WidgetConventions.GetWidgetFullName(typeInfo),
ShortName = WidgetConventions.GetWidgetName(typeInfo),
Type = typeInfo.AsType()
};
return descriptor;
}
示例15: CreateDescriptor
private static ViewComponentDescriptor CreateDescriptor(TypeInfo typeInfo)
{
var candidate = new ViewComponentDescriptor
{
FullName = ViewComponentConventions.GetComponentFullName(typeInfo),
ShortName = ViewComponentConventions.GetComponentName(typeInfo),
TypeInfo = typeInfo,
MethodInfo = FindMethod(typeInfo.AsType())
};
return candidate;
}