Click here to Skip to main content
15,891,951 members
Home / Discussions / C#
   

C#

 
RantRe: About Reflecting and generics Pin
PozzaVecia14-Feb-12 12:58
PozzaVecia14-Feb-12 12:58 
GeneralRe: About Reflecting and generics Pin
GParkings14-Feb-12 23:44
GParkings14-Feb-12 23:44 
GeneralRe: About Reflecting and generics Pin
PozzaVecia15-Feb-12 0:10
PozzaVecia15-Feb-12 0:10 
GeneralRe: About Reflecting and generics Pin
GParkings15-Feb-12 0:59
GParkings15-Feb-12 0:59 
GeneralRe: About Reflecting and generics Pin
jschell14-Feb-12 13:40
jschell14-Feb-12 13:40 
AnswerRe: About Reflecting and generics Pin
AspDotNetDev14-Feb-12 13:00
protectorAspDotNetDev14-Feb-12 13:00 
GeneralRe: About Reflecting and generics Pin
PozzaVecia14-Feb-12 21:20
PozzaVecia14-Feb-12 21:20 
AnswerRe: About Reflecting and generics Pin
AspDotNetDev14-Feb-12 21:49
protectorAspDotNetDev14-Feb-12 21:49 
Still not sure exactly what problem you are having, but here is an example much like yours (I compiled and ran it to confirm it works):
C#
string myKeyType = "System.String";
string myValueType = "System.Double";
Type myType = typeof(KeyValuePair<,>);
Type[] typeArgs = { Type.GetType(myKeyType), Type.GetType(myValueType) };
Type fullType = myType.MakeGenericType(typeArgs);
object newInstance = Activator.CreateInstance(fullType, new object[] { "testKey", 5 });
KeyValuePair<string, double> casted = (KeyValuePair<string, double>)newInstance;
MessageBox.Show(casted.Key + "_" + casted.Value.ToString());


Note that you must use the full type in the strings and it must have the correct casing (uppercase/lowercase). If you are getting "string" and "double", you must first convert them to "System.String" and "System.Double" first. If you wanted, there are also ways to scan an assembly to see all of the possible types it provides, which would allow you to, for example, do a case-insensitive scan for types which contain "double". You could then use that type in your code. However, I wouldn't recommend that... I'd say you should make sure your data is correct (your data in this case being the strings that represent the types).

GeneralRe: About Reflecting and generics Pin
PozzaVecia14-Feb-12 22:26
PozzaVecia14-Feb-12 22:26 
GeneralRe: About Reflecting and generics Pin
AspDotNetDev15-Feb-12 7:09
protectorAspDotNetDev15-Feb-12 7:09 
GeneralRe: About Reflecting and generics Pin
AspDotNetDev14-Feb-12 21:52
protectorAspDotNetDev14-Feb-12 21:52 
AnswerRe: About Reflecting and generics Pin
BobJanova14-Feb-12 23:20
BobJanova14-Feb-12 23:20 
GeneralRe: About Reflecting and generics Pin
PozzaVecia14-Feb-12 23:29
PozzaVecia14-Feb-12 23:29 
GeneralRe: About Reflecting and generics Pin
BobJanova15-Feb-12 0:50
BobJanova15-Feb-12 0:50 
GeneralRe: About Reflecting and generics Pin
PozzaVecia15-Feb-12 1:24
PozzaVecia15-Feb-12 1:24 
GeneralRe: About Reflecting and generics Pin
BobJanova15-Feb-12 1:59
BobJanova15-Feb-12 1:59 
GeneralRe: About Reflecting and generics Pin
PozzaVecia15-Feb-12 2:30
PozzaVecia15-Feb-12 2:30 
GeneralRe: About Reflecting and generics Pin
BobJanova15-Feb-12 2:37
BobJanova15-Feb-12 2:37 
GeneralRe: About Reflecting and generics Pin
GParkings15-Feb-12 2:52
GParkings15-Feb-12 2:52 
GeneralRe: About Reflecting and generics Pin
GParkings15-Feb-12 2:54
GParkings15-Feb-12 2:54 
GeneralRe: About Reflecting and generics Pin
PozzaVecia15-Feb-12 3:09
PozzaVecia15-Feb-12 3:09 
GeneralRe: About Reflecting and generics Pin
GParkings15-Feb-12 3:25
GParkings15-Feb-12 3:25 
GeneralRe: About Reflecting and generics Pin
BobJanova15-Feb-12 3:40
BobJanova15-Feb-12 3:40 
GeneralRe: About Reflecting and generics Pin
PozzaVecia15-Feb-12 10:17
PozzaVecia15-Feb-12 10:17 
GeneralRe: About Reflecting and generics Pin
BobJanova15-Feb-12 21:57
BobJanova15-Feb-12 21:57 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.