How to Instantiate Business Objects in Blazor Wasm RRS feed

  • Question

  • User-343630552 posted

    I have a set of Business Objects from which I would like to instantiate a dynamic subset of as part of a Blazor Wasm application.  I can of course instantiate all of them "just in case" or write a big switch statement to do them selectively.  But I would rather use reflection:  Assembly.GetExecutingAssembly().CreateInstance(class_file_path, ...). 

    The problem seems to be that I don't know how to get the path to the class file.  Within Visual Studio Community 2019, the files are in a folder within wwwroot.  I tried Directory.GetCurrentDirectory(), which returned only '/'.   So, any idea if/how I can make this work?

    Thursday, July 23, 2020 7:42 PM


All replies

  • User475983607 posted

    That's what delegates and interfaces are for.

    Thursday, July 23, 2020 11:09 PM
  • User-343630552 posted
    Thanks for the quick reply. I’m afraid I don’t understand what you are suggesting. I have used delegates and interfaces in the past and have read some more about them after getting your reply. But am not seeing the solution. Perhaps that is because I don’t know how to use these constructs as you are suggesting or I wasn’t clear enough in describing my need.

    On the latter, I have 20 or so classes, a number of which contain quite a few private and public methods, and all of which encapsulate the data related to the specific instance of the object as instantiated and subsequently changed via method calls. For many of the classes, I need the ability to create multiple instances at once.

    If delegates and interfaces provide a way to do this, can you please explain a bit more? I hope I’m not being a dunce.
    Friday, July 24, 2020 11:26 AM
  • User475983607 posted

    You are throwing around  OOP terms but nothing concrete.  We cannot see the code or how your code is organized.   I can only guess how your code works.  Your latest post seems like you need a factory pattern.

    Anyway, Interfaces are used in Dependency Injection to inject a concrete types.  Blazor WASM supports DI and so does ASP.NET Core in general.  https://docs.microsoft.com/en-us/aspnet/core/blazor/fundamentals/dependency-injection?view=aspnetcore-3.1.  DI should allow you to initialize the types you need within pages, classes, etc.  

    Is that what you're after, DI?  Maybe a DI factory?

    Friday, July 24, 2020 12:01 PM
  • User-343630552 posted

    I'm sorry that I'm still not being clear.  The particular statement I have been trying to get working is shown below.  I have used this successfully in a WinForms app to instantiate objects via reflection.  It is returning null in the Blazor Wasm app.  I hope I don't need to show a demo copy of one of the business classes.  They are simply public classes in the same namespace as the rest of the app and have constructors that assign values to private variables based on the argument(s) supplied to the constructor.  If this isn't clear enough, then I will simply have to keep researching what you are suggesting to see if I can understand.

    Per your latest message, I use DI to inject the types.  That's how I am able to write "myBO = new myBOClass(args);", which is what I will do if I can't use reflection to do the same thing without a 20 case switch statement that would need to be altered if/when I add a new BOClass.   These types are not used in the UI, but rather invoked by user action in the UI.

    Thanks again. Steve

    myBO = Assembly.GetExecutingAssembly().CreateInstance(pathToBOClass,
    true, BindingFlags.Default, null, myConstructorArgument, null, null);

    Friday, July 24, 2020 1:41 PM
  • User475983607 posted

    Share demo code that illustrates the technical problem you are trying to solve.

    Friday, July 24, 2020 2:28 PM
  • User-474980206 posted

    Blazor webassembly runtime is .netstandard 2.1. It does not support filenames


    you should use the type name. Because it’s .netstandard, any method is free to return not implemented, so you will need to check.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Saturday, July 25, 2020 2:06 AM
  • User-343630552 posted

    Thanks, Bruce.  This is a classic example of "look but do not see."  There were numerous signs in front of my face that said to use the type name, not the file name, including the very descriptive variable names I used in my WinForms code from which I copied the CreateInstance statement and the documentation you referenced which I read at least twice.  Mental blocks are a pain.  

    Saturday, July 25, 2020 12:14 PM