none
[UWP]Create instance of class from string RRS feed

  • Question

  • Hello

    i am creating an application that will have many classes that may or may not get used at runtime.  

    For example.   Let’s say I have a Car class.  

    But at runtime I don’t need to use it. 

    But then the user selects use Car class and gives it a Name.  

    I want to create a instance of the class from the name and use its methods based on the name of that got instantiated.  

    I also may need to create multiple instances of the class all with different names.  

    Is this possible and does anyone have a sample of how I could do this. 


    Monday, August 12, 2019 1:03 PM

All replies

  • I think you are trying to allow the user to write source code that is then compiled. No, that is not possible. There is a solution to your fundamental problem but you do not describe that so we cannot help with that.


    Sam Hobbs
    SimpleSamples.Info

    Monday, August 12, 2019 7:05 PM
  • No that is not a what I am trying to do. For example. The program I am working on is going to have a number of classes that will or will not be used. Each class is basically a device driver. I don’t want to use the Singleton method as I may have more that 1 device that the class can control. Scenario: I have in the solution: Device model 1 Device model 2 Device model 3 Device model 4 If I want to use some in one room and not in others or the same device type in multiple locations.
    Monday, August 12, 2019 7:24 PM
  • Hi,

    Based on your description, you need a class that could be named in the runtime and at the meantime, all the methods inside the class should be renamed according to the name. I don't think it's possible to do that. Perhaps a class with a name parameter will be better.

    Best regards,

    Roy


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, August 14, 2019 3:23 AM
    Moderator
  • Maybe I as not giving a good description.

    This program will be used in home automation.

    Within the solution I will have a number of drivers(Classes) that can control a variety of devices. (this part I can already have).

    What I need is to be able to use a class more than once(create new instances) based on weather I add that device to a location.

    A better example would be :

    I have a driver for a Sharp TV.

    I create one class for this device.

    Now when the user is setting up the system they add 1 Sharp to the Living Room and one to the Master Bedroom.

    Each have a different IP address and will get a name such as "Living Room TV" or "Bedroom TV".  

    I want to be able t create two instances of the sharp class with different IPs.


    Wednesday, August 14, 2019 10:38 AM
  • Add an IP parameter for the class? Done.
    • Edited by MasterFree Wednesday, August 14, 2019 2:19 PM
    Wednesday, August 14, 2019 2:19 PM
  • I will try this but not sure how it will work. I need to be able to send commands to specific ones by name or I guess address could work if I make a file that stores name to ip. But how would I create the instance based on that. Can’t say. Device (somename) = new Device();
    Friday, August 16, 2019 1:12 AM
  • Hi,

    What about pass the name to the Construction method like Device somename= new Device(somename)?

    Best regards,

    Roy


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, August 20, 2019 4:19 AM
    Moderator
  • Roy,  that is what I would like to do.

    let say I have a device class of Lamp.

    The user says "I have 3 Lamps."

    they want to name the Lamps ( living room, bedroom, kitchen)

    what I want to do is not only store these names in a text file(this part I got)

    but also then create new instances of Lamp based on the names.

    for example(based on the names above):

    Lamp living_room = new Lamp();

    Lamp bedroom = new Lamp();

    Lamp kitchen = new Lamp();

    Tuesday, August 20, 2019 10:18 AM
  • Take a look at reflection

    https://stackify.com/what-is-c-reflection/

    https://docs.microsoft.com/en-us/dotnet/api/system.activator.createinstance?view=netframework-4.8

    https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/concepts/reflection

    Basically, you will want to have a dictionary of some sort, which maps the strings in your UI to types of your classes. Then you can create a new instance of that class. Furthermore, you then set values for properties in your newly created object based on names, without ever having to use the "new" operator, or know in advance the type of the object.

    My special approach to this is to use attributes to map properties and types directly to UI elements.

    • Edited by mcosmin Wednesday, August 21, 2019 7:05 PM
    Wednesday, August 21, 2019 7:02 PM
  • Thank you.

    I will look into this option.

    Reflection is really new to me and I have never really used it.

    Could you possibly show an example based on my sample above.

    In the meantime I will look at this and see if I can figure it out based on your suggestion.

    Thursday, August 22, 2019 12:21 AM