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?

  • What are the strings? Are they perhaps serialized objects? – zimdanen Nov 16 '12 at 20:04
  • 5
    `>` Huh? Are you sure about that? – Jon B Nov 16 '12 at 20:06
  • Is your code set to guarantee that `DeleteThisStruct` is already created by `ProjectA` when you action the delete from `ProjectB`? – Bob. Nov 16 '12 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" – Alexandr Grey Nov 16 '12 at 20:09
  • Ok, I simplified it. Lets tackle one problem at a time. I made is simple DeleteThisStruct – Alexandr Grey Nov 16 '12 at 20:20
  • possible duplicate of [Pass An Instantiated System.Type as a Type Parameter for a Generic Class](http://stackoverflow.com/questions/266115/pass-an-instantiated-system-type-as-a-type-parameter-for-a-generic-class) – nawfal Jan 17 '14 at 16:24

1 Answers1

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).

fsimonazzi
  • 2,975
  • 15
  • 13
  • 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 where, however "TKey" and "TValue" are simple string values and not actual types? – Alexandr Grey Nov 16 '12 at 22:19
  • 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. – fsimonazzi Nov 16 '12 at 22:25
  • 1
    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. – fsimonazzi Nov 16 '12 at 22:27
  • I want this: `DeleteThisStruct x = new DeleteThisStruct;` 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). – Alexandr Grey Nov 16 '12 at 22:27
  • see http://stackoverflow.com/questions/232535/how-to-use-reflection-to-call-generic-method – fsimonazzi Nov 16 '12 at 22:28
  • You cannot write that code as you want. But you can write it in a generic method on TKey and TValue, and invoke *that* method using reflection. – fsimonazzi Nov 16 '12 at 22:29
  • E.g. void DoStuff(whatever) { var x = new DeleteThisStruct(); // do something with x } And invoke that method as explained in the answer I linked – fsimonazzi Nov 16 '12 at 22:30