0

I need to cast like this:

/* Cast Brakets */                    
(Type.FromString(mystring))myObject.doSomething();

I need to get the type that I cast at runtime, in this particular case I can't use a generic "T" class or method.

Although if you had a way of changing the T-generic Type at runtime (without calling a class or method again) that would also help.

Here is a more specific version of my problem:

I have a List comboList and my GUI can add new ComboBoxes at runtime, these will automatically be put into comboList. Now there are database tables for each of those ComboBoxes in comboList. I added the name of each of the database classes in the .Tag field of each ComboBox

(example: combobox_users.Tag = "users_databaseClass")

Now at runtime I need to cast into the type of users_databaseClass for example, but it appears to me that this is impossible. It appears that this is simply not possible to put a Type into cast-brackets at runtime to actually cast that type.

Help would be greatly appreciated, I've been trying for days.

Aiken
  • 1
  • 1
  • 2
    Once you cast an object to a type, what are you planning to do with it? Do you simply want to call a method that is not available on the object, or is there something else? – Sergey Kalinichenko May 08 '12 at 00:28
  • Reference for getting a type from a string: http://stackoverflow.com/questions/721870/c-sharp-how-can-i-get-type-from-a-string-representation – Evan Mulawski May 08 '12 at 00:29
  • Dup - ? http://stackoverflow.com/questions/1089123/setting-a-property-by-reflection-with-a-string-value – Micah Armantrout May 08 '12 at 00:29
  • @dasblinkenlight: I want to do only that, I need access to the objects parameters and methods after I casted it at runtime. – Aiken May 08 '12 at 00:39
  • To clear this up, I don't need the type, you can't cast with a type (as far as I understand it). I need the... the thing that you put into cast-brackets to cast to it. It should be a class-name that is in the current namespace, I'm not even sure how to call that. – Aiken May 08 '12 at 00:41
  • @Aiken, why would you need to do that? Casting is useless if you don't know the type statically. The reason for casting is to be able to call members of the type you cast to, but in this case you couldn't do it, since you wouldn't know which members are available. Explain why you need to cast, and perhaps we will be able to help you find a better approach. – Thomas Levesque May 08 '12 at 00:47
  • 1
    @Aiken If that's all you want, cast to `dynamic` and call whatever you feel like: `object myObj = ...; ((dynamic)myObj).SomeMethod();` – Sergey Kalinichenko May 08 '12 at 00:49
  • possible duplicate of [c# casting to type gotten from typename as string](http://stackoverflow.com/questions/1187319/c-sharp-casting-to-type-gotten-from-typename-as-string) – Kendall Frey May 08 '12 at 00:53
  • @ThomasLevesque: I want to cast an object from a foreach loop depending on a string-parameter from another class. If the string is "user" I want to cast to "user". So the comboBox (mentioned above) would be filled by the right databasetable. – Aiken May 08 '12 at 00:53
  • @dasblinkenlight: I'm going to look into that, I'm fairly sure I need to use the string somehow, since that's the only way I get the information, but give me a few. – Aiken May 08 '12 at 00:55
  • @Aiken, you still don't explain *why* you need to cast. As I said, it doesn't make sense to cast to a type you don't know statically. – Thomas Levesque May 08 '12 at 01:01
  • @ThomasLevesque: I don't understand why it doesn't make sense to do this. I could write a switch-case statement to do what I want, but my boss wants me to do it completely generic, so we can add new ComboBoxes to the Form at runtime. Since new ComboBoxes need to be bound to database-tables, and I only have the names of the database-tables as string, I need to cast into those classes to work with the database-classes. – Aiken May 08 '12 at 01:36
  • @Aiken what UI framework are you using? In general, you display objects in a ComboBox by calling the ToString method (no need to cast; it's a method of `object`). – phoog May 08 '12 at 02:04
  • @Aiken, what type would you declare the result of the cast? You can't declare a variable unless you know its type (or you can declare it as object, but then you can't access the members of the specific type) – Thomas Levesque May 08 '12 at 03:13

1 Answers1

0

To use the properties and methods of a wacky type, you'll need a reference to that type (e.g. Type.GetType("string_name_of_type")) and you'll need some pretty cool reflection (e.g. theType.GetMethod("method_name")). Your performance won't be great. See http://msdn.microsoft.com/en-us/library/system.type.getmethod(v=vs.71).aspx

robrich
  • 13,017
  • 7
  • 36
  • 63