• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 896
  • Last Modified:

List<myObject.GetType()> lstStuff Concept - Please Help

I am trying to do something like List<String> lstStuff without having to use the keyword "String". The objects that will go through this are dynamic and i can't reference it's type by name. I've tried things such as List<myObject.GetType()> or List<typeof(myObject)> but that give a compiler error. Are there any ways to pass the generic type without using the actual object's type name? Thanks. This is extremly urgant. i'm trying to implement a fix for a system that can only be brought down for a few hours.
Thanks.

Oh, as a side note the actual method i am trying this on isn't "List". I am using "List" as a popular example. It's really a custom method. Parse<T>(T objectToParse)

Thank you so much!

~B
0
used2could
Asked:
used2could
  • 4
  • 2
  • 2
3 Solutions
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
Have you tried using Object?...
0
 
used2couldAuthor Commented:
Thanks Idle Mind,
I can't use Object because the actions applied to the passed in Type are specific to the Type being used.
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
If they are specific to that Type then you kinda have to pass that type in!...  =\

Otherwise, you could create an Interface and declare it of that Type.  Then anything class that Implements that Interface can be passed into the method.

Another option would be to pass it all in as the generic Object type and attempt to get the "specific" parts out via Reflection.
0
Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

 
used2couldAuthor Commented:
There are too many different types that could be passed in. I'm going to have to use objects and reflection. I didn't want to go that route but oh well. Just for the record, there is no way to use List<GetType()> for generics?

Thanks
0
 
gregoryyoungCommented:
You could use reflection ...

Get an instance of the open generic method Parse<> (generic method definition) in this case then use MakeGenericMethod http://msdn.microsoft.com/en-us/library/system.reflection.methodinfo.makegenericmethod.aspx on it to create a MethodInfo that is your call to make ... then invoke the call through your method info .... voila

There is an example of this process here http://msdn.microsoft.com/en-us/library/system.reflection.methodinfo.makegenericmethod.aspx

Cheers,

Greg
0
 
gregoryyoungCommented:
@used2could of course not generics are strongly typed at compile time.
0
 
used2couldAuthor Commented:
Thanks guys!! i started re-writing it to accommodate objects and allow the method using generics to still access them via overloading to keep other things from breaking. Thanks for the clarifications!
0
 
used2couldAuthor Commented:
I'll be inspecting the member's details via reflection. That should do it. Thanks!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

  • 4
  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now