0

I have the following code

var type = Type.GetType(BrowseDialogName);
var browseForm = Activator.CreateInstance(type) as MyBrowseForm<SPORTS>;

I'm using entity framework and SPORTS is the name of the poco class

my problem here is that this code is inside a custom textbox that has a button and I'm not able to get <T> , at runtime I have the name of the poco class.

so I was wondering if I could do the casting at runtime.

any help or guide is very appreciated

maccettura
  • 10,514
  • 3
  • 28
  • 35
Asım Gündüz
  • 1,257
  • 3
  • 17
  • 42
  • If using C# 7: https://stackoverflow.com/questions/43080505/c-sharp-7-0-switch-on-system-type – TyCobb Mar 28 '18 at 20:09
  • 2
    Lets say you were allowed to cast it. What then. What are you going to do with the result of that cast that you couldn't have done before? – Servy Mar 28 '18 at 20:11
  • @Servy, the text box has a button, when clicked on the button opens a browse form where when I can search the SPOTS table where I can search by name, ref no, or other table properties. select the value and returns it to the textbox. I want to use this control instead of combobox when I have a lot of records. (sorry for bad english) – Asım Gündüz Mar 28 '18 at 20:21
  • 1
    @Licentia But how could you possibly expect to write code that uses that type when you don't know what type it's going to be, as it could be *any* type? – Servy Mar 28 '18 at 20:36
  • @servy the text box has an information panel which is a user control and has a datasource on it, say I have a customer and countries 1to1 I make a browseform for countries, the browse forms have search add edit idle buttons. So I create a search form for bıth pocos among with their Crud forms. Then for countries I create a user control that displays country name and continent. After that on the textbox Properties I type usercontrol name because it has datasource on it I get the poco name at runtime and set it to create the browse form email me for details a.gunduz@live.com – Asım Gündüz Mar 28 '18 at 21:16
  • That doesn't answer my question at all. Again, what are you actually going to do with this variable once you've cast it that you couldn't have done without casting it. – Servy Mar 28 '18 at 21:19
  • @servy oh now I get it:) I could create it and cast it to dynamic and call the showdialog.. well it's midnight and been working since 9 thx for pointing it out :)) – Asım Gündüz Mar 28 '18 at 21:24
  • @Licentia If you know it's a `Form` and you want to call `ShowDialog` then cast it to a `Form` and remove all of the dynamic code entirely. – Servy Mar 28 '18 at 21:27
  • @Servy yes I could do that but there are a few more properties I set in there, I did't post that part of the code because it was unnecessary, so if casted to form I would still need reflection to access the properties – Asım Gündüz Mar 28 '18 at 21:31
  • @Licentia If a bunch of objects are all expected to provide a certain set of functionality then they should all implement an interface. – Servy Mar 28 '18 at 21:32
  • Have you thought about creating a base form class that has these few properties in it and then derive your other forms from that? Then you wont need to use reflection. Just check if derives from the base and then cast it to the base if it does. then your stuff is available in intellisense too. – Kevbo Mar 29 '18 at 20:06

0 Answers0