I have a function similar to this one and i can't edit it:
internal object DoSomething(Type type, object obj)
I need to pass type as type of an ObservableCollection but T is unknown at design time.
And this is not enough:
Type t = typeof(ObservableCollection<>);
How can i solved it?
EDIT
When using LiteDb you can map POCO class properties with LiteDb objects. By default, an ObservableCollection returns an Array. I need to change this default behavior passing an ObservableCollectio and get back a BsonDocument
This code works:
BsonMapper.Global.RegisterType<ObservableCollection<Phone>>
(serialize: (ObservableCollection) => OCToDoc(Client.Phones),
deserialize: (BsonDocument) => new ObservableCollection<Phone>()
);
public BsonDocument OCToDoc<T>(ObservableCollection<T> oc)
{
BsonDocument doc = new BsonDocument();
Type t = typeof(T);
BsonDocument item = new BsonDocument();
doc.Add(t.Name, item);
foreach (PropertyInfo pi in t.GetProperties())
{
string key = pi.Name;
item.Add(key, new BsonValue());
}
return doc;
}
RegisterType from LiteDb.dll is:
public void RegisterType<T>(Func<T, BsonValue> serialize, Func<BsonValue, T> deserialize);
public void RegisterType(Type type, Func<object, BsonValue> serialize, Func<BsonValue, object> deserialize);
I need to make a generic mapping for whichever type of ObservableCollection. This means that
ObservableCollection<Phone>
must be
ObservableCollection<T>
where T isn't known at runtime. So, how to pass an ObservableCollection in RegisterType<...> and in OCToDoc(...)