0

Lets say I have

public partial class DeleteThisStruct<TKey, TValue>

However, values of TKey and TValue I would have to read as strings from a file (I already have a method to do that, so lets say I already have 2 strings) Example: "MyNamespace.MyClass" and "MyNamespace.MyClass2"

2nd problem is that code which need to instantiate this object is called from a different project but in the same solution. Lets say I am instantiating in ProjectA, while DeleteThisStruct is in ProjectB (which is included in references to ProjectA)

How would I, using generics or not, would dynamically instantiate DeleteThisStruct and get a member value from it?

6
  • What are the strings? Are they perhaps serialized objects? Commented Nov 16, 2012 at 20:04
  • 5
    <TKey, TValue<TOtherKey, TOtherValue>> Huh? Are you sure about that? Commented Nov 16, 2012 at 20:06
  • Is your code set to guarantee that DeleteThisStruct is already created by ProjectA when you action the delete from ProjectB? Commented Nov 16, 2012 at 20:07
  • DeleteThisStruct is a class definition which is already there. I obviously substituted TKey, TValue for real classes, but yes, this is a structure I am working with Strings are classnames with namespaces. Example: "MyNamespace.MyClass" Commented Nov 16, 2012 at 20:09
  • Ok, I simplified it. Lets tackle one problem at a time. I made is simple DeleteThisStruct<TKey,TValue> Commented Nov 16, 2012 at 20:20

1 Answer 1

1

Look at the "Constructing an Instance of a Generic Type" section in http://msdn.microsoft.com/en-us/library/b8ytshk6.aspx. You will need to retrieve the type objects based on the strings, but be aware that just "MyNamespace.MyClass" won't be enough to retrieve the necessary type objects in general. Without the assembly name you can only get types from the mscorlib assembly and the current assembly (http://msdn.microsoft.com/en-us/library/w3f99sx1.aspx).

Sign up to request clarification or add additional context in comments.

7 Comments

That example would give you o as an object. In order to make it useful you need to cast it. And THIS IS where the problem comes in. How would you cast resulting object "o" to a DeleteThisStruct<TKey, TValue> where, however "TKey" and "TValue" are simple string values and not actual types?
Cast it to what? You don't have a type known at compile time to cast to. What code do you expect to write? Either define a non-generic base class you can cast to that exposes what you need to access or you will have to resort to reflection.
A technique that sometimes helps is to write all the code you need, including the instantiation, as a generic method for your generic parameters. And instead of building and instantiating a generic type, you build an invocation to the generic method and invoke it using reflection; from then on it will all be just plain code.
I want this: DeleteThisStruct<TKey, TValue> x = new DeleteThisStruct<TKey, TValue>; However TKey and TValue are "System.String" for example and are not known compile time. Object of type "object" are useless (at least for what I need to do).
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.