本文整理汇总了C#中System.Type.GetGenericTypeDefinition方法的典型用法代码示例。如果您正苦于以下问题:C# Type.GetGenericTypeDefinition方法的具体用法?C# Type.GetGenericTypeDefinition怎么用?C# Type.GetGenericTypeDefinition使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Type
的用法示例。
在下文中一共展示了Type.GetGenericTypeDefinition方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: GetTypeName
private static string GetTypeName(Type array)
{
var element = array.IsArray
? array.GetElementType()
: array.IsGenericType && (array.GetGenericTypeDefinition() == typeof(List<>)
|| array.GetGenericTypeDefinition() == typeof(HashSet<>))
? array.GetGenericArguments()[0]
: null;
if (element == null)
return null;
if (element == typeof(string))
return "\"-NGS-\".CLOB_ARR";
if (element == typeof(bool) || element == typeof(bool?))
return "\"-NGS-\".BOOL_ARR";
if (element == typeof(int) || element == typeof(int?))
return "\"-NGS-\".INT_ARR";
if (element == typeof(decimal) || element == typeof(decimal?))
return "\"-NGS-\".NUMBER_ARR";
if (element == typeof(long) || element == typeof(long?))
return "\"-NGS-\".LONG_ARR";
if (element == typeof(float) || element == typeof(float?))
return "\"-NGS-\".FLOAT_ARR";
if (element == typeof(double) || element == typeof(double?))
return "\"-NGS-\".DOUBLE_ARR";
if (element == typeof(DateTime) || element == typeof(DateTime?))
return "\"-NGS-\".TWTZ_ARR";
if (element == typeof(Guid) || element == typeof(Guid?))
return "\"-NGS-\".GUID_ARR";
if (typeof(IAggregateRoot).IsAssignableFrom(element))
return "\"" + element.Namespace + "\".\"-" + element.Name + "-EA-\"";
if (typeof(IAggregateRoot).IsAssignableFrom(element))
return "\"" + element.Namespace + "\".\"-" + element.Name + "-SA-\"";
//TODO missing value types
return null;
}
示例2: GetClosedParameterType
public static Type GetClosedParameterType(this AbstractTypeEmitter type, Type parameter)
{
if (parameter.IsGenericTypeDefinition)
{
return parameter.GetGenericTypeDefinition().MakeGenericType(type.GetGenericArgumentsFor(parameter));
}
if (parameter.IsGenericType)
{
var arguments = parameter.GetGenericArguments();
if (CloseGenericParametersIfAny(type, arguments))
{
return parameter.GetGenericTypeDefinition().MakeGenericType(arguments);
}
}
if (parameter.IsGenericParameter)
{
return type.GetGenericArgument(parameter.Name);
}
if (parameter.IsArray)
{
var elementType = GetClosedParameterType(type, parameter.GetElementType());
return elementType.MakeArrayType();
}
if (parameter.IsByRef)
{
var elementType = GetClosedParameterType(type, parameter.GetElementType());
return elementType.MakeByRefType();
}
return parameter;
}
示例3: CreateDataSource
protected override object CreateDataSource( string name, Type type, NamedParameters ctorArgs )
{
var dataSources = (IList)typeof( List<> ).CreateGeneric( type );
foreach ( var factory in Factories )
{
if ( factory.CanCreate( name, type ) )
{
dataSources.Add( factory.Create( name, type, ctorArgs ) );
}
}
var innerType = type.GetGenericArguments().First();
if ( type.GetGenericTypeDefinition() == typeof( ISingleDataSource<> ) )
{
return typeof( StackSingleDataSource<> ).CreateGeneric( innerType, name, dataSources );
}
if ( type.GetGenericTypeDefinition() == typeof( IEnumerableDataSource<> ) )
{
return typeof( StackEnumerableDataSource<> ).CreateGeneric( innerType, name, dataSources );
}
else
{
throw new NotSupportedException();
}
}
示例4: InMemoryEventSourcedRepositoryStrategy
internal static Func<PocketContainer, object> InMemoryEventSourcedRepositoryStrategy(Type type, PocketContainer container)
{
if (type.IsGenericType &&
(type.GetGenericTypeDefinition() == typeof (IEventSourcedRepository<>) ||
type.GetGenericTypeDefinition() == typeof (InMemoryEventSourcedRepository<>)))
{
var aggregateType = type.GenericTypeArguments.Single();
var repositoryType = typeof (InMemoryEventSourcedRepository<>).MakeGenericType(aggregateType);
var streamName = AggregateType.EventStreamName(aggregateType);
// get the single registered event stream instance
var stream = container.Resolve<ConcurrentDictionary<string, IEventStream>>()
.GetOrAdd(streamName,
name => container.Clone()
.Register(_ => name)
.Resolve<IEventStream>());
return c => Activator.CreateInstance(repositoryType, stream, c.Resolve<IEventBus>());
}
if (type == typeof (IEventStream))
{
return c => c.Resolve<InMemoryEventStream>();
}
return null;
}
示例5: Resolve
/// <summary>
/// Returns any bindings from the specified collection that match the specified service.
/// </summary>
/// <param name="bindings">The multimap of all registered bindings.</param>
/// <param name="service">The service in question.</param>
/// <returns>The series of matching bindings.</returns>
public IEnumerable<IBinding> Resolve(Multimap<Type, IBinding> bindings, Type service)
{
if (!service.IsGenericType || !bindings.ContainsKey(service.GetGenericTypeDefinition()))
return Enumerable.Empty<IBinding>();
return bindings[service.GetGenericTypeDefinition()].ToEnumerable();
}
示例6: Of
IEnumerable<MessageOwner> IMessageOwnersSelector.Of(Type type)
{
if(type.IsGenericType && (type.GetGenericTypeDefinition() == typeof(Request<>) || type.GetGenericTypeDefinition() == typeof(Reply<>)))
return Of(type.GetGenericArguments()[0]);
return Of(type);
}
示例7: Create
public static Item Create(Type type)
{
if (type == typeof(string))
{
return new StringItem();
}
if (type == typeof(float))
{
return new FloatItem();
}
if (type == typeof(double))
{
return new DoubleItem();
}
else if (type.GetCustomAttributes(typeof(CubeHack.Data.EditorDataAttribute), false).Length != 0)
{
return new ObjectItem(type);
}
else if (type.IsGenericType && type.GetGenericTypeDefinition() == typeof(Dictionary<,>) && type.GetGenericArguments()[0] == typeof(string))
{
return new DictionaryItem(type.GetGenericArguments()[1]);
}
else if (type.IsGenericType && type.GetGenericTypeDefinition() == typeof(List<>))
{
return new ListItem(type.GetGenericArguments()[0]);
}
else
{
throw new ArgumentException("type");
}
}
示例8: CanConvert
public override bool CanConvert(Type objectType)
{
var canConvert = objectType.IsGenericType
&& (objectType.GetGenericTypeDefinition() == typeof(Dictionary<,>) || objectType.GetGenericTypeDefinition() == typeof(IDictionary<,>))
&& objectType.GetGenericArguments()[0] == typeof(Medicine);
return canConvert;
}
示例9: Resolve
/// <summary>
/// Returns any bindings from the specified collection that match the specified service.
/// </summary>
/// <param name="bindings">The multimap of all registered bindings.</param>
/// <param name="service">The service in question.</param>
/// <returns>The series of matching bindings.</returns>
public IEnumerable<IBinding> Resolve(IDictionary<Type, IEnumerable<IBinding>> bindings, Type service)
{
if (!service.GetTypeInfo().IsGenericType || service.GetTypeInfo().IsGenericTypeDefinition || !bindings.ContainsKey(service.GetGenericTypeDefinition()))
return Enumerable.Empty<IBinding>();
return bindings[service.GetGenericTypeDefinition()];
}
示例10: CreateInstance
/// <summary>
/// Create a new instance from a Type
/// </summary>
public static object CreateInstance(Type type)
{
try
{
CreateObject c = null;
if (_cacheCtor.TryGetValue(type, out c))
{
return c();
}
else
{
if (type.IsClass)
{
var dynMethod = new DynamicMethod("_", type, null);
var il = dynMethod.GetILGenerator();
il.Emit(OpCodes.Newobj, type.GetConstructor(Type.EmptyTypes));
il.Emit(OpCodes.Ret);
c = (CreateObject)dynMethod.CreateDelegate(typeof(CreateObject));
_cacheCtor.Add(type, c);
}
else if (type.IsInterface) // some know interfaces
{
if (type.IsGenericType && type.GetGenericTypeDefinition() == typeof(IList<>))
{
return CreateInstance(GetGenericListOfType(UnderlyingTypeOf(type)));
}
else if (type.IsGenericType && type.GetGenericTypeDefinition() == typeof(IDictionary<,>))
{
var k = type.GetGenericArguments()[0];
var v = type.GetGenericArguments()[1];
return CreateInstance(GetGenericDictionaryOfType(k, v));
}
else
{
throw LiteException.InvalidCtor(type);
}
}
else // structs
{
var dynMethod = new DynamicMethod("_", typeof(object), null);
var il = dynMethod.GetILGenerator();
var lv = il.DeclareLocal(type);
il.Emit(OpCodes.Ldloca_S, lv);
il.Emit(OpCodes.Initobj, type);
il.Emit(OpCodes.Ldloc_0);
il.Emit(OpCodes.Box, type);
il.Emit(OpCodes.Ret);
c = (CreateObject)dynMethod.CreateDelegate(typeof(CreateObject));
_cacheCtor.Add(type, c);
}
return c();
}
}
catch (Exception)
{
throw LiteException.InvalidCtor(type);
}
}
示例11: CanConvert
public override bool CanConvert(Type objectType)
{
return objectType.IsGenericType
&&
(objectType.GetGenericTypeDefinition() == typeof(RawOrQueryDescriptor<>)
|| objectType.GetGenericTypeDefinition() == typeof(RawOrFilterDescriptor<>));
}
示例12: GetLazyFactory
/// <summary>
/// Get a ILazyLoadProxy for a type, member name
/// </summary>
/// <param name="type">The target type which contains the member proxyfied</param>
/// <returns>Return the ILazyLoadProxy instance</returns>
public ILazyFactory GetLazyFactory(Type type)
{
if (type.IsInterface)
{
if (type.IsGenericType && (type.GetGenericTypeDefinition() == typeof(IList<>)))
{
return _factory[type.GetGenericTypeDefinition()] as ILazyFactory;
}
else
if (type == typeof(IList))
{
return _factory[type] as ILazyFactory;
}
else
{
throw new DataMapperException("Cannot proxy others interfaces than IList or IList<>.");
}
}
else
{
// if you want to proxy concrete classes, there are also two requirements:
// the class can not be sealed and only virtual methods can be intercepted.
// The reason is that DynamicProxy will create a subclass of your class overriding all methods
// so it can dispatch the invocations to the interceptor.
return new LazyLoadProxyFactory();
}
}
示例13: GetAggregator
/// <summary>
/// Get a function that coerces a sequence of one type into another type.
/// This is primarily used for aggregators stored in ProjectionExpression's, which are used to represent the
/// final transformation of the entire result set of a query.
/// </summary>
public static LambdaExpression GetAggregator(Type expectedType, Type actualType)
{
Type actualElementType = TypeHelper.GetElementType(actualType);
if (!expectedType.IsAssignableFrom(actualType))
{
Type expectedElementType = TypeHelper.GetElementType(expectedType);
ParameterExpression p = Expression.Parameter(actualType, "p");
Expression body = null;
if (expectedType.IsAssignableFrom(actualElementType))
{
body = Expression.Call(typeof(Enumerable), "SingleOrDefault", new Type[] { actualElementType }, p);
}
else if (expectedType.IsGenericType
&&
(expectedType == typeof(IQueryable) || expectedType == typeof(IOrderedQueryable)
|| expectedType.GetGenericTypeDefinition() == typeof(IQueryable<>)
|| expectedType.GetGenericTypeDefinition() == typeof(IOrderedQueryable<>)))
{
body = Expression.Call(
typeof(Queryable), "AsQueryable", new Type[] { expectedElementType }, CoerceElement(expectedElementType, p));
if (body.Type != expectedType)
{
body = Expression.Convert(body, expectedType);
}
}
else if (expectedType.IsArray && expectedType.GetArrayRank() == 1)
{
body = Expression.Call(
typeof(Enumerable), "ToArray", new Type[] { expectedElementType }, CoerceElement(expectedElementType, p));
}
else if (expectedType.IsGenericType && expectedType.GetGenericTypeDefinition().IsAssignableFrom(typeof(IList<>)))
{
var gt = typeof(DeferredList<>).MakeGenericType(expectedType.GetGenericArguments());
var cn =
gt.GetConstructor(new Type[] { typeof(IEnumerable<>).MakeGenericType(expectedType.GetGenericArguments()) });
body = Expression.New(cn, CoerceElement(expectedElementType, p));
}
else if (expectedType.IsAssignableFrom(typeof(List<>).MakeGenericType(actualElementType)))
{
// List<T> can be assigned to expectedType
body = Expression.Call(
typeof(Enumerable), "ToList", new Type[] { expectedElementType }, CoerceElement(expectedElementType, p));
}
else
{
// some other collection type that has a constructor that takes IEnumerable<T>
ConstructorInfo ci = expectedType.GetConstructor(new Type[] { actualType });
if (ci != null)
{
body = Expression.New(ci, p);
}
}
if (body != null)
{
return Expression.Lambda(body, p);
}
}
return null;
}
示例14: resolveGenericTypeDefinition
private static Type resolveGenericTypeDefinition(Type parent)
{
var shouldUseGenericType = !(parent.IsGenericType && parent.GetGenericTypeDefinition() != parent);
if (parent.IsGenericType && shouldUseGenericType)
parent = parent.GetGenericTypeDefinition();
return parent;
}
示例15: Resolve
/// <summary>
/// Returns any bindings from the specified collection that match the specified service.
/// </summary>
/// <param name="bindings">The multimap of all registered bindings.</param>
/// <param name="service">The service in question.</param>
/// <returns>The series of matching bindings.</returns>
public IEnumerable<IBinding> Resolve(Multimap<Type, IBinding> bindings, Type service)
{
if (!service.IsGenericType || !bindings.ContainsKey(service.GetGenericTypeDefinition()))
yield break;
Type gtd = service.GetGenericTypeDefinition();
foreach (IBinding binding in bindings[gtd])
yield return binding;
}