Get key values of a generic Dictionary <,> using reflection - c#

How can I get the key values of a generic IDictionary<,> using reflection.
This is the type of thing I want to do.
public static string Format<T>(T item)
{
if (item.GetType().GetInterfaces().Any(x => x.IsGenericType && x.GetGenericTypeDefinition() == typeof(IDictionary<,>)))
{
// I know it's a IDictionary so figure out what the types are
Type keyType = item.GetType().GetGenericArguments()[0];
Type valueType = item.GetType().GetGenericArguments()[1];
//Now cast it to the correct IDictionary. How do I properly inject the type here?
var keyList = ((IDictionary<keyType, valueType>)item).Select(x => x.Key.ToString()).ToArray<string>();
}
}
Edit: Clarified that I want to use reflection

I think you are overcomplicating things. When I look at your code, you are trying to do the following:
myDictionary.Keys.Select(k => k.ToString()).ToArray();
So,
public string Format<T>(T value) {
if (##item is dictionary) {
var items = name.GetType().GetProperty("Keys", BindingFlags.Instance | BindingFlags.Public).GetValue(item) as IEnumerable;
if (items == null) throw new ArgumentException("Dictionary with no keys?");
string[] data = items.OfType<object>().Select(o => o.ToString()).ToArray();
}
}

Unfortunately you cannot pass a System.Type as a type argument. If you want to use the IDictionary<,> properties, you need to do a little more reflection.
var dictionaryType = typeof(IDictionary<,>).MakeGenericType(keyType, valueType);
var keysProperty = dictionaryType.GetProperty("Keys");
var keys = ((IEnumerable)keysProperty.GetValue(item)).OfType<object>().Select(k => k.ToString()).ToArray<string>();
However, if all you want are the keys, you can just use the non-generic IDictionary interface, and don't bother with reflection.
var dictionary = item as IDictionary;
if (dictionary != null)
{
var keyList = dictionary.Select(x => x.Key.ToString()).ToArray<string>();
}

You got a few problems here. One minor issue is you don't use the found interface for getting the generic arguments.
The other bigger problem is you're trying to use dynamically found types in types defined statically at compile time (IDictionary<keyType, valueType>).
If you want to continue down this road it's using reflection, which can be hard. Depending on your goal you could choose another path and try this out:
public static string Format<TKey,TValue>(IDictionary<TKey,TValue> item)
{
var keyList = item.Select(x => x.Key.ToString()).ToArray();
// do some work with keyList and return a string.
}
This will still work public static string Format<T>(T item) handling other types, so implementing this beside it:
public static string Format<T>(T item)
{
// handle non IDictionary<,> objects here
}
Making you able to call format anywhere:
Format(new Dictionary<string,int> { { "hello world", 1337 } });
Format("string");
Format(new { Hello = "World" });

The issue is that you're trying to get generic code parameters from Type objects. This is not directly possible, because Type is a class like nay other, it just represents a class, and type arguments are used to compile the method. Thus, you can't get new type arguments within the underlying code...
That being said, You could add extra generic parameters, with specific constraints,
public static Format<T, TKey, TValue>(T item)
where T : IDictionary<TKey, TValue>
Or, to be more succinct
public static Format<TKey, TValue>(IDicitonary<TKey, TValue> item)
This can then be called using dynamic so as to ensure the proper arguments are used
public static string Format<T>(T item)
{
if (item.GetType().GetInterfaces().Any(x => x.IsGenericType && x.GetGenericTypeDefinition() == typeof(IDictionary<,>)))
{ FormatDictionary((dynamic) item); }
}
private static FormatDictionary<TKey, TValue>(IDicitonary<TKey, TValue> item)
If you must perform these calls with Type objects, you'll have do so via metacode - either with more reflection calls, or by compiling a method using Expressions

Related

Add items to any object of ICollection<T> using reflection

I'm trying to support mapping to/from any kind of collection that implements ICollection<T> via reflection, because ICollection<T> requires implementation of the Add method.
This works fine for most common collection types, but fails for edge cases like LinkedList<T> where the Add method is hidden and can only be called by casting the LinkedList<T> to ICollection<T>.
However it's not possible to convert to ICollection<> because it is not covariant.
The other option I was considering was searching for both implicit and explicit implementations of Add, but I don't see any information on how to do this when the interface is generic?
What would be the correct approach to take?
Updated to show code snippet where I'm reflecting from xml to object mapping.
private object CollectionXmlNodeListToObject(
XmlNodeList nodeList, System.Type collectionType)
{
// this is not possible because ICollection<> is not covariant
object collection = Convert.ChangeType(
CreateInstanceOfType(collectionType), ICollection<>);
Type containedType = collectionType.GetTypeInfo().GenericTypeArguments[0];
foreach (XmlNode node in nodeList)
{
object value = CreateInstanceOfType(containedType);
if (containedType.IsClass && MetaDataCache.Contains(containedType))
value = ToObject(value, node, node.Name);
else
value = node.InnerText;
// this throws NullReferenceException when the type is LinkedList,
// because this is explicitly implemented in LinkedList
collectionType.GetMethod("Add")
.Invoke(collection, new[] { value });
}
return collection;
}
I am writing a small framework to map from object to xml using class and property attributes. So I cannot use generics because all of this is being done at runtime.
I initially was checking for IEnumerable before, but ran into other oddities with it (strings implement IEnumerable and are immutable) that I decided it was safest to stick to ICollection<>
With explicit interface implementation, the object has all the interface methods, but the object's Type does not.
So here's how to add an item to a LinkedList<T>, or any ICollection<T> through reflection:
var ll = new LinkedList<int>();
var t = typeof(int);
var colType = typeof(ICollection<>).MakeGenericType(t);
var addMethod = colType.GetMethod("Add");
addMethod.Invoke(ll, new object[] { 1 });
This functionality is met at compile-time using the Cast<T>() method. You just need a run-time version, which is pretty straightforward:
static public object LateCast(this ICollection items, Type itemType)
{
var methodDefintionForCast = typeof(System.Linq.Enumerable)
.GetMethods(BindingFlags.Static | BindingFlags.Public)
.Where(mi => mi.Name == "Cast")
.Select(mi => mi.GetGenericMethodDefinition())
.Single(gmd => gmd != null && gmd.GetGenericArguments().Length == 1);
var method = methodDefintionForCast.MakeGenericMethod(new Type[] { itemType });
return method.Invoke(null, new[] { items });
}
Now you can take any non-generic collection and make it generic at run-time. For example, these two are equivalent:
var list = nodeList.Cast<XmlNode>();
object list = nodeList.LateCast(typeof(XmlNode));
And you can convert a whole collection with this:
static public IEnumerable ConvertToGeneric(this ICollection source, Type collectionType)
{
return source.LateCast(collectionType.GetGenericArguments()[0]) as IEnumerable;
}
object list = nodeList.ConvertToGeneric(nodeList, typeof(ICollection<XmlNode>));
This solution works with linked lists as well as all the other collection types.
See my working example on DotNetFiddle
Pretty much all .NET collections take an IEnumerable<T> as the constructor, so you could make use of that:
private static object CollectionXmlNodeListToObject(System.Type collectionType)
{
// T
Type containedType = collectionType.GetTypeInfo().GenericTypeArguments[0];
// List<T>
Type interimListType = typeof(List<>).MakeGenericType(containedType);
// IEnumerable<T>
Type ienumerableType = typeof(IEnumerable<>).MakeGenericType(containedType);
IList interimList = Activator.CreateInstance(interimListType) as IList;
interimList.Add(null);
interimList.Add(null);
interimList.Add(null);
interimList.Add(null);
// If we can directly assign the interim list, do so
if (collectionType == interimListType || collectionType.IsAssignableFrom(interimListType))
{
return interimList;
}
// Try to get the IEnumerable<T> constructor and use that to construct the collection object
var constructor = collectionType.GetConstructor(new Type[] { ienumerableType });
if (constructor != null)
{
return constructor.Invoke(new object[] { interimList });
}
else
{
throw new NotImplementedException();
}
}
Try it online
Obviously you could optimise this by moving the list population to another method, and then maybe use your existing method as far as you can, and then use this where you can't.

How to compare two types of classes which receives generics? [duplicate]

I've got a generic method:
Func<IEnumerable<T>, bool> CreateFunction<T>()
where T can be any number of different types. This method does a bunch of stuff using reflection and if T is an IDictionary, regardless of the the dictionary's TKey and TValue I need to execute dictionary specific code.
So the method could be called:
var f = CreateFunction<string>();
var f0 = CreateFunction<SomePocoType>();
var f1 = CreateFunction<IDictionary<string,object>>();
var f2 = CreateFunction<Dictionary<string,object>>();
var f3 = CreateFunction<SomeDerivedDictionaryType<string,object>>();
etc.
Clarification per #Andy's answer
Ultimately I want to know if T inherits from/implements IDictionary even if T itself is Dictionary or some other type that derives from that interface.
if(typeof(T) == typeof(IDictionary<,>)
doesn't work because T is the generic type not the generic type definition.
And without knowing TKey and TValue (which are not known at compile time) I can't do a comparison to any concrete type that I would know about until runtime.
The only thing that I've come up with are looking at the type's name or inspecting its method with reflection, looking for methods that would lead me to believe it is a dictionary (i.e. look for ContainsKey and get_Item).
Is there any straightforward way to make this sort of determination?
You can avoid using ugly and potentially risky type name string checking using the IsGenericType and GetGenericTypeDefinition members, as follows:
var type = typeof (T);
if (typeof (IDictionary).IsAssignableFrom(type))
{
//non-generic dictionary
}
else if (type.IsGenericType &&
type.GetGenericTypeDefinition() == typeof (IDictionary<,>))
{
//generic dictionary interface
}
else if (type.GetInterfaces().Any(
i => i.IsGenericType &&
i.GetGenericTypeDefinition() == typeof (IDictionary<,>)))
{
//implements generic dictionary
}
The easy way is just this:
Type iDict = null;
if (typeof(T).GetGenericTypeDefinition() == typeof(IDictionary<,>))
iDict = typeof(T);
else
iDict = typeof(T).GetInterface(typeof(IDictionary<,>).Name);
if (iDict != null)
{
var genericParams = iDict.GetGenericArguments();
Type tKey = genericParams[0], tValue = genericParams[1];
}
Note that this will not work (throws an exception) if T implements more than one IDictionary<,> interface, but that will probably be fine for your purposes.
For the sake of completeness, here's an implementation that will work on types with multiple IDictionary<,> interfaces by using the first one:
Type iDict = t.GetType().GetInterfaces()
.Where(t => t.IsGenericType
&& t.GetGenericTypeDefinition() == typeof(IDictionary<,>))
.FirstOrDefault();
if (iDict != null)
{
var genericParams = iDict.GetGenericArguments();
Type tKey = genericParams[0], tValue = genericParams[1];
}
Note that in this second routine t is an object, whereas T is a type in the first routine.
You could do something like
class Program
{
static void Main(string[] args)
{
Example<IDictionary<int, string>>.IsDictionary();
Example<SortedDictionary<int, string>>.IsDictionary();
Example<Dictionary<int, string>>.IsDictionary();
Console.ReadKey();
}
}
public class Example<T>
{
public static void IsDictionary()
{
if (typeof(T).GetInterface(typeof(IDictionary<,>).Name) != null || typeof(T).Name.Contains("IDictionary"))
{
Console.WriteLine("Is IDictionary");
}
else
{
Console.WriteLine("Not IDictionary");
}
}
}
I think that if you call Type.GetGenericTypeDefinition() that should return the "base" generic type used to construct the concrete Type.
Note that just comparing this to IDictionary<,> is likely not enough, because if someone passes in an instance of Dictionary<,> I assume you would want to use that, as well. You could either check to see if the Type implements IDictionary<,> or you might be able to call Type.IsAssignableFrom(), although based on the doc I'm not sure how well this would work with generic Types.

C# - Make a generic foreach method that accepts any type?

Im busy updating an entity using entity framework and web api (on the PUT method of the controller). For each collection property on the updated object, I loop through and check if each item exists in the collection on the existing object or not. If not, I add it.
The trouble is I have a lot of collections on the object and I find myself repeating the following code many times over.
Is there a way for me to wrap this into a generic method and pass that method the 2 collections to compare? Maybe by specifying the name of the property to check and primary key? How would I be able to specify the type for the foreach loop for example?
foreach (HBGender gender in updated.HBGenders)
{
HBGender _gender = existing.HBGenders.FirstOrDefault(o => o.GenderID == gender.GenderID);
if (_gender == null)
{
//do some stuff here like attach and add
}
}
return existing; //return the modified object
Thanks in advance. I hope this makes sense.
In its simplest form you could write an extension method as such:
public static class IEnumerableExtensionMethods
{
public static ICollection<T> ForEachAndAdd<T>(this IEnumerable<T> self,
ICollection<T> other,
Func<T, T, bool> predicate) where T : class
{
foreach(var h1 in self)
{
if(other.FirstOrDefault(h2 => predicate(h1, h2)) == null)
other.Add(h1);
}
return other;
}
}
Usage:
List<HBGender> updated = new List<HBGender>();
List<HBGender> existing = new List<HBGender<();
return updated.ForEachAndAdd(existing, (h1, h2) => h1.Gender == h2.Gender);
Note that if there is extra logic needed during an add, you could add an additonal Action<T> parameter to do so.
I don't know what you are trying to do, but you can play with this example:
List<object> a = new List<object>();
a.Add("awgf");
a.Add('v');
a.Add(4);
foreach (object b in a)
{
Type type = b.GetType().//Select more usefull
Convert.ChangeType(object,type);
}
Just pass your existing check function, as an extra parameter
public List<Class1> Find(List<Class1> updated, List<Class1> existing, Func<Class1, bool> predicate)
{
foreach (Class1 gender in updated)
{
Class1 _gender = existing.FirstOrDefault(predicate); //predicate for quoted example will be o => o.GenderID == gender.GenderID
if (_gender == null)
{
//do some stuff here like attach and add
}
}
return existing;
}

Having trouble with Generics in this .NET code

i'm trying to make a mixed collection of Types. I know the types at the start.. but I can't seem to figure out the syntax to make the collection, etc.
eg.
....
// I leave the typo there, for embarrassment :(
Initialize(new []{ typeof(Cat), typeof(Dog), typeof(JohnSkeet) });
...
public Foo Initialize(IEnumerable<Type> types)
{
// for each type, set up the inmemory storage.
foreach(var type in types)
{
// ????
// Create an empty list, which will only contain this 'type'
// I'm guessing, an IDictionary<type, ICollection<type>>().. thingy ?
}
}
public ICollection<Type> SomeTypeData(Type type)
{
// Return the collection, for this type.
}
Does this mane sense? Is this possible?
Okay, now that I think I know what you want, it would look something like this:
// This can't really be *properly* statically typed
private readonly Dictionary<Type, object> typeMap = new
Dictionary<Type, object>();
public Foo Initialize(IEnumerable<Type> types)
{
Type genericListType = typeof(List<>);
foreach(var type in types)
{
// MakeGenericType is really badly named
Type constructedListType = genericListType.MakeGenericType(type);
typeMap[type] = Activator.CreateInstance(constructedListType);
}
}
// We can't express this particularly safely either,
// although we *could* return the non-generic IList
public object SomeTypeData(Type type)
{
return typeMap[type];
}
// This *is* statically typed, although we need to cast inside
public IList<T> SomeTypeData<T>()
{
return (IList<T>) typeMap[typeof(T)];
}
See this blog post for a similar example.
Note that basically you're trying to represent something which generics simply can't handle, in terms of the internal dictionary type... and the first form of SomeTypeData can't be statically typed either... because that means knowing the type at compile time when we'll only actually be given it at execution time.
It looks to me like you're trying to create some kind of instance repository; a class that stores a list of instances of a given type.
Here's an example implementation. I've included both a generic and non-generic version of the SomeTypeData method:
public class InstanceRepository
{
private IDictionary<Type, ICollection> _Instances = new Dictionary<Type, ICollection>();
public ICollection SomeTypeData(Type type)
{
ICollection instanceList;
if (!_Instances.TryGetValue(type, out instanceList))
{
// this type does not exist in our dictionary, so let's create a new empty list
// we could do this:
//instanceList = new List<object>();
// but let's use reflection to make a more type-specific List<T> instance:
instanceList = (ICollection)Activator.CreateInstance(typeof(List<>).MakeGenericType(type));
// now add it to the dictionary
_Instances.Add(type, instanceList);
}
// Return the collection, for this type.
return instanceList;
}
public IList<T> SomeTypeData<T>()
{
Type type = typeof(T);
ICollection instanceList;
if (!_Instances.TryGetValue(typeof(T), out instanceList))
{
instanceList = new List<T>();
_Instances.Add(type, instanceList);
}
// here we are assuming that all of the lists in our dictionary implement IList<T>.
// This is a pretty safe assumption, since the dictionary is private and we know that
// this class always creates List<T> objects to put into the dictionary.
return (IList<T>)instanceList;
}
}
Below is a usage example:
Generic:
InstanceRepository repository = new InstanceRepository();
var listOfCats = repository.SomeTypeData<Cat>();
listOfCats.Add(new Cat());
Cat firstCat = listOfCats[0];
Console.WriteLine(listOfCats.GetType().FullName);
Non-Generic:
InstanceRepository repository = new InstanceRepository();
var listOfCats = (IList<Cat>)repository.SomeTypeData(typeof(Cat));
listOfCats.Add(new Cat());
Cat firstCat = listOfCats[0];
Console.WriteLine(listOfCats.GetType().FullName);
I guess you want something like
_dict[typeof(Cat)]=new List<Cat>();
_dict[typeof(Dog)]=new List<Dog>();
only programatically based on given types?
Something like this should work:
public void Initialize(IEnumerable<Type> types)
{
foreach(var type in types)
{
var list = Activator.CreateInstance(Type.GetType("System.Collections.Generic.List`1").MakeGenericType(type));
_cache[type] = list;
}
}
public ICollection<T> Get<T>()
{
object list;
if (_cache.TryGetValue(typeof(T), out list)
{
return list as ICollection<T>;
}
else
{
...
}
}
var cats = Get<Cat>();
I'm not sure I fully understand you're question, but if you already have an IEnumerable<Type> which contains an enumeration of Type objects, then why not just use that to initialize some type of Collection (such as List<Type>)?
public ICollection<Type> Initialize(IEnumerable<Type> types)
{
ICollection<Type> collection = new List<Type>(types);
return collection;
}

If object is Generic List

Is there any way to determine if an object is a generic list? I'm not going to know the type of the list, I just know it's a list. How can I determine that?
This will return "True"
List<int> myList = new List<int>();
Console.Write(myList.GetType().IsGenericType && myList is IEnumerable);
Do you care to know if it's exactly a "List"... or are you ok with it being IEnumerable, and Generic?
The following method will return the item type of a generic collection type.
If the type does not implement ICollection<> then null is returned.
static Type GetGenericCollectionItemType(Type type)
{
if (type.IsGenericType)
{
var args = type.GetGenericArguments();
if (args.Length == 1 &&
typeof(ICollection<>).MakeGenericType(args).IsAssignableFrom(type))
{
return args[0];
}
}
return null;
}
Edit: The above solution assumes that the specified type has a generic parameter of its own. This will not work for types that implement ICollection<> with a hard coded generic parameter, for example:
class PersonCollection : List<Person> {}
Here is a new implementation that will handle this case.
static Type GetGenericCollectionItemType(Type type)
{
return type.GetInterfaces()
.Where(face => face.IsGenericType &&
face.GetGenericTypeDefinition() == typeof(ICollection<>))
.Select(face => face.GetGenericArguments()[0])
.FirstOrDefault();
}
The accepted answer doesn't guarantee the type of IList<>.
Check this version, it works for me:
private static bool IsList(object value)
{
var type = value.GetType();
var targetType = typeof (IList<>);
return type.GetInterfaces().Any(i => i.IsGenericType
&& i.GetGenericTypeDefinition() == targetType);
}
Try:
if(yourList.GetType().IsGenericType)
{
var genericTypeParams = yourList.GetType().GetGenericArguments;
//do something interesting with the types..
}
The question is ambiguous.
The answer depends on what you mean by a generic list.
A List<SomeType> ?
A class that derives from List<SomeType> ?
A class that implements IList<SomeType> (in which case an array can be considered to be a generic list - e.g. int[] implements IList<int>)?
A class that is generic and implements IEnumerable (this is the test proposed in the accepted answer)? But this will also consider the following rather pathological class to be a generic list:
.
public class MyClass<T> : IEnumerable
{
IEnumerator IEnumerable.GetEnumerator()
{
return null;
}
}
The best solution (e.g. whether to use GetType, IsAssignableFrom, etc) will depend on what you mean.
Theres a GetType() function in the System.Object class. Have you tried that?

Categories