[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

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

Posted on 2008-11-04
8
Medium Priority
?
893 Views
Last Modified: 2013-12-17
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
Comment
Question by:used2could
  • 4
  • 2
  • 2
8 Comments
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 22882001
Have you tried using Object?...
0
 

Author Comment

by:used2could
ID: 22882092
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
 
LVL 86

Accepted Solution

by:
Mike Tomlinson earned 1000 total points
ID: 22882149
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
Restore individual SQL databases with ease

Veeam Explorer for Microsoft SQL Server delivers an easy-to-use, wizard-driven interface for restoring your databases from a backup. No expert SQL background required. Web interface provides a complete view of all available SQL databases to simplify the recovery of lost database

 

Author Comment

by:used2could
ID: 22882319
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
 
LVL 37

Assisted Solution

by:gregoryyoung
gregoryyoung earned 1000 total points
ID: 22882326
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
 
LVL 37

Assisted Solution

by:gregoryyoung
gregoryyoung earned 1000 total points
ID: 22882333
@used2could of course not generics are strongly typed at compile time.
0
 

Author Comment

by:used2could
ID: 22882402
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
 

Author Closing Comment

by:used2could
ID: 31513338
I'll be inspecting the member's details via reflection. That should do it. Thanks!
0

Featured Post

Granular recovery for Microsoft Exchange

With Veeam Explorer for Microsoft Exchange you can choose the Exchange Servers and restore points you’re interested in, and Veeam Explorer will present the contents of those mailbox stores for browsing, searching and exporting.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
High user turnover can cause old/redundant user data to consume valuable space. UserResourceCleanup was developed to address this by automatically deleting user folders when the user account is deleted.
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…
When cloud platforms entered the scene, users and companies jumped on board to take advantage of the many benefits, like the ability to work and connect with company information from various locations. What many didn't foresee was the increased risk…
Suggested Courses

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question