定义泛型类 public class Class1<T> { void A() { } list<T>(T t) { }} 调用,Class1<string>.A() , Class1<int>.A()
public static TPrototype Resolve<TPrototype, TDefault>(string resolverName = null) where TPrototype : class where TDefault : TPrototype, new() { if (ObjectManagerConfigurations.TypeMapping != null) { IDictionary<string, Type> resolvers; if (ObjectManagerConfigurations.TypeMapping.TryGetValue(typeof(TPrototype), out resolvers)) { Type type; resolverName = (resolverName==null) ? string.Empty : resolverName.Trim(); if (resolvers.TryGetValue(resolverName, out type)) { try { return Activator.CreateInstance(type) as TPrototype; } catch (Exception ex) { Trace.WriteLine(string.Format("failed to create instance of type \"{0}\". Exception:{1}",type.FullName, ex.ToString())); } } } } return new TDefault(); }
这是msdn上的,能结合我上面的代码给一份吗?
{
void A()
{
}
list<T>(T t)
{
}}
调用,Class1<string>.A() , Class1<int>.A()
where TPrototype : class
where TDefault : TPrototype, new()
{
if (ObjectManagerConfigurations.TypeMapping != null)
{
IDictionary<string, Type> resolvers;
if (ObjectManagerConfigurations.TypeMapping.TryGetValue(typeof(TPrototype), out resolvers))
{
Type type;
resolverName = (resolverName==null) ? string.Empty : resolverName.Trim();
if (resolvers.TryGetValue(resolverName, out type))
{
try
{
return Activator.CreateInstance(type) as TPrototype;
}
catch (Exception ex)
{
Trace.WriteLine(string.Format("failed to create instance of type \"{0}\". Exception:{1}",type.FullName, ex.ToString()));
}
}
}
}
return new TDefault();
}