c# - Getting the parameters of a generic type into a dictionary -


i have written method iterates on generic parameter types of instance...

dictionary<string, guid> returns

tkey: system.string

tvalue: system.guid

tuple<guid, bool, stringbuilder> returns

t1: system.guid

t2: system.boolean

t3: system.text.stringbuilder

method

public static dictionary<string, type> getgenericparameters(type type) {     dictionary<string, type> result = new dictionary<string, type>();     type[] tvalues = type.getgenericarguments();     type[] tkeys = type.getgenerictypedefinition().getgenericarguments();     if (tvalues.length == tkeys.length)     {         (int index = 0; index < tvalues.length; index++)         {             result.add(tkeys[index].name, tvalues[index]);         }     }     return result; } 

test:

foreach (keyvaluepair<string, type> pair in getgenericparameters(typeof(tuple<string, int, guid, stringbuilder, long, bool, tuple<bool, string>>))) {     console.writeline("{0}: {1}", pair.key, pair.value); } 

result:

t1: system.string

t2: system.int32

t3: system.guid

t4: system.text.stringbuilder

t5: system.int64

t6: system.boolean

t7: system.tuple`2[system.boolean,system.string]

it seems work ok...but gut feeling there cases logic broken, , written more efficient...possibly linq or something...any ideas on how can improve , ensure doesn't break?


Comments

Popular posts from this blog

java - Intellij Synchronizing output directories .. -

git - Initial Commit: "fatal: could not create leading directories of ..." -