110,533
社区成员
发帖
与我相关
我的任务
分享
public class Manager
{
private Dictionary<Type, Collection<Person>> mDic = new Dictionary<Type, Collection<Person>>();
public void Add<T>(Collection<T> value) where T : Person
{
mDic.Add(typeof(T), value);
}
public Collection<T> Get<T>(T owner) where T : Person
{
var value = mDic[typeof(T)];
return value;
}
}
public class Collection<T> where T : Person
{
}
public class Person
{
public string FirstName { get; set; }
public string LastName { get; set; }
}
public class Employee : Person
{
}
public Collection<T> Get<T>(T owner) where T : Person
{
var value = mDic[typeof(T)];
return (Collection<T>)value;
}
public class Manager<T>
where T :class
{
private Dictionary<Type, Collection<T>> mDic = new Dictionary<Type, Collection<T>>();
public void Add(Collection<T> value)
{
mDic.Add(typeof(T), value);
}
public Collection<T> Get(T owner)
{
Collection<T> value = mDic[typeof(T)];
return value;
}
}
public class Collection<T>
{
}
public class Person
{
private string _firstName;
public string FirstName
{
get { return _firstName; }
set { _firstName = value; }
}
private string _lastName;
public string LastName
{
get { return _lastName; }
set { _lastName = value; }
}
}
public class Employee : Person
{
}
public class Manager
{
private Dictionary<Type, IEnumerable<Person>> mDic = new Dictionary<Type, IEnumerable<Person>>();
public void Add<T>(IEnumerable<T> value) where T : Person
{
mDic.Add(typeof(T), value);
}
public MyCollection<T> Get<T>() where T : Person
{
var value = mDic[typeof(T)];
return (MyCollection<T>)value;
}
}
return new Collection<T>(value.Cast<T>().ToList());
using System.Linq;
//..
public Collection<T> Get<T>(/* T owner */) where T : Person
{
if (mDic.ContainsKey(typeof(T)){
var value = mDic[typeof(T)];
if(value != null) return value.Cast<T>();
return null;
}
}
class Manager
{
private Dictionary<Type, List<Person>> mDic = new Dictionary<Type, List<Person>>();
public void Add(IEnumerable<Person> coll)
{
foreach (Person p in coll)
{
Type t = p.GetType();
List<Person> list;
if (!mDic.TryGetValue(t, out list))
{
list = new List();
mDic.Add(t, list);
}
list.Add(p);
}
}
public T[] Get<T>() where T: Person
{
Type t = typeof(T);
List<Person> list;
if (mDic.TryGetValue(t, out list))
return list.Cast<T>().ToArray();
else
return new T[0];
}
}