Page 1 of 2 12 LastLast
Results 1 to 10 of 12

Thread: Create instance of non-local class

  1. #1
    Join Date
    Jul 2008
    Location
    North Carolina, USA
    Posts
    6

    Default Create instance of non-local class

    In my calling app I need to create an instance of a class called Main that is located in a class library called Class1.

    Can someone please provide a simple example of what my object definition what would like for the following case...

    This is obviously wrong, but for example...
    <object name="MyObject" type="c:\some-where-else\Class1.dll Class1.Main Class1></object>

    Thanks in advance...

  2. #2
    Join Date
    Oct 2005
    Location
    Belgium
    Posts
    213

    Default

    Make sure that your calling app either references the Class1 class library or the dll is in the bin folder.

    Then you can use this object definition

    <object name="MyObject" type="Class1.Main, Class1 />

    So in type you specify the full type name, followed by the assembly

  3. #3
    Join Date
    Jul 2008
    Location
    North Carolina, USA
    Posts
    6

    Default

    Thanks for the response, however...

    If I have to set a reference to a library (that is not in the path of the calling app), I see no benefit in using Spring.

    I thought one of the benefits to using this framework was late binding.

    If I have to copy a library to the same folder as the calling app, this seems a bit awkward. What about the dependencies that this library may require ? If it requires additional DLLs to be in the same folder then I have to copy those as well.

    Surely there is a better solution.

    Anyone else have an idea ?

  4. #4
    Join Date
    Oct 2005
    Location
    Belgium
    Posts
    213

    Default

    You don't need to add a reference, make sure the dll can be found. Meaning it's in the same folder as the running application or in the gac. (or any other way .Net searches for assemblies)

    Spring allows you to have loose coupling between the components that form your application. If one of those components depends on component A and B, then yes they need to be there as well.

    Take a look at the samples.

  5. #5
    Join Date
    Jul 2008
    Location
    North Carolina, USA
    Posts
    6

    Default

    So, Spring is not able to create an instance of a class in class library that is not located in the "searchable" path of the calling app ?

  6. #6
    Join Date
    Oct 2005
    Location
    Belgium
    Posts
    213

    Default

    I don't think anything can and I'm pretty sure, if it were possible, it would be error prone.

    If that dll, that you somehow references, changes, it's quite possible it'll break your application.

  7. #7
    Join Date
    Jul 2008
    Location
    North Carolina, USA
    Posts
    6

    Default

    I guess you didn't know that you can do this with pure .NET, here's an example...

    'Create the assembly object
    asAssembly = Reflection.Assembly.LoadFrom(<path to the dll> & "something.dll")

    'Get the assembly's type
    tClassType = asAssembly.GetType("<class name to create>")

    'Constructor arguments, if required
    Dim args(0) As Object
    args(0) = Nothing

    'Create new object as type
    oTestClass = Activator.CreateInstance(tClassType, args)

    'Call any functions or properties as needed
    oTestClass.ShowConfiguration()

    Broken interfaces is always a problem, even when using Spring (if I am not wrong).

    Does anyone else have a suggestion for how to use Sping to create an instance of a class in a class library that is not in the searchable path of the calling app ?

  8. #8
    Join Date
    Oct 2005
    Location
    Belgium
    Posts
    213

    Default

    I'm quite aware of it, I just never have had the need for it.

    If you want to add additional subfolders that contain dlls, you can add these in the app.config see MSDN here.

    If they are outside of the scope of your app, I don't know how to do it. Have you tried loading the assemblies into your application domain before running any spring.net related stuff?

  9. #9
    Join Date
    Oct 2005
    Location
    Belgium
    Posts
    213

    Default

    I stepped through the creation of objects in the application context, if you are not using fully qualified assembly names Spring will look in the loaded assemblies of the application domain to resolve the type. I think that's what you were after?

    Code:
     static void Main(string[] args)
            {
                Assembly.LoadFile(@"C:\Dev\SpringDynamicLoad\SpringDynamicLoad\ClassLibrary1\bin\Debug\ClassLibrary1.dll");
                object o = ContextRegistry.GetContext().GetObject("MyObject");
            }
    HTML Code:
     <objects xmlns="http://www.springframework.net" >
          <object id="MyObject"
                  type="ClassLibrary1.Class1">
            <property name="Test" value="MyTestValue"/>
          </object>
        </objects>

  10. #10
    Join Date
    Jul 2008
    Location
    North Carolina, USA
    Posts
    6

    Default

    OK, that works.

    For my purposes (late binding to a class with events), it seems that this can be done with pure .NET and Spring.

    I am new to using Spring, What can you tell me that would convince me that I should use Spring and not my own code ?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •