Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. how to avoid references

how to avoid references

Scheduled Pinned Locked Moved C#
tutorialquestion
12 Posts 4 Posters 2 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    Mephisto187
    wrote on last edited by
    #1

    I want to use a dll without manually adding a reference in the solution explorer. Is there any way to do this ?? Something corresponding to the good old "import" maybe ???

    H J P 3 Replies Last reply
    0
    • M Mephisto187

      I want to use a dll without manually adding a reference in the solution explorer. Is there any way to do this ?? Something corresponding to the good old "import" maybe ???

      H Offline
      H Offline
      Heath Stewart
      wrote on last edited by
      #2

      An assembly cannot reference a Type if the assembly in which that Type is defined is not loaded. This is no different than any other platform. Even #import requires that a typelib exists on the machine (and running the code requires that the libraries containing the types defined in the typelib are present on the system). Once an assembly that is required is referenced by your project, you can use using SomeNamespace; at the top of your source file, preventing you from having to type the namespace for a class each time, but the project still needs to have the assembly loaded that contains the definition for the Type you want to use.

      -----BEGIN GEEK CODE BLOCK----- Version: 3.21 GCS/G/MU d- s: a- C++++ UL@ P++(+++) L+(--) E--- W+++ N++ o+ K? w++++ O- M(+) V? PS-- PE Y++ PGP++ t++@ 5 X+++ R+@ tv+ b(-)>b++ DI++++ D+ G e++>+++ h---* r+++ y+++ -----END GEEK CODE BLOCK-----

      M 1 Reply Last reply
      0
      • H Heath Stewart

        An assembly cannot reference a Type if the assembly in which that Type is defined is not loaded. This is no different than any other platform. Even #import requires that a typelib exists on the machine (and running the code requires that the libraries containing the types defined in the typelib are present on the system). Once an assembly that is required is referenced by your project, you can use using SomeNamespace; at the top of your source file, preventing you from having to type the namespace for a class each time, but the project still needs to have the assembly loaded that contains the definition for the Type you want to use.

        -----BEGIN GEEK CODE BLOCK----- Version: 3.21 GCS/G/MU d- s: a- C++++ UL@ P++(+++) L+(--) E--- W+++ N++ o+ K? w++++ O- M(+) V? PS-- PE Y++ PGP++ t++@ 5 X+++ R+@ tv+ b(-)>b++ DI++++ D+ G e++>+++ h---* r+++ y+++ -----END GEEK CODE BLOCK-----

        M Offline
        M Offline
        Mephisto187
        wrote on last edited by
        #3

        I totally agree with you, but that´s not the point :confused: The studio can create references to any registered component on the system. So I´m sure there is a way to do the same !?

        H 1 Reply Last reply
        0
        • M Mephisto187

          I totally agree with you, but that´s not the point :confused: The studio can create references to any registered component on the system. So I´m sure there is a way to do the same !?

          H Offline
          H Offline
          Heath Stewart
          wrote on last edited by
          #4

          What do you mean, "The studio can create references to any registered component on the system."? Your project needs to reference the assemblies required by your project, period. When you add components, the assemblies in which that component and its dependencies are defined are added automatically if they aren't added already, but only when you use the designer. If you manually type the code, it does not do this automatically. It's all because of the designer. If you're talking about the assemblies that show up in the Add Reference dialog, you can add additional directories in which to search in the HKEY_LOCAL_MACHINE\Software\Microsoft\.NETFramework\Assembly folders key. If you don't want to worry about where assemblies are located (even though VS.NET will copy assemblies to the application directory that aren't installed in the GAC), you can use gacutil.exe to add assemblies to the Global Assembly Cache (GAC), or drag-n-drop the assemblies into the %WINDIR%\assembly directory. This is no different than any other language or platform, as I said before. In Java, the classes or JAR files have to be in the CLASSPATH when compiling or running the application (you can override the CLASSPATH on the command line, though). In Win32, libs must be linked in when compiling and the DLLs must either be in the application directory or in a directory in the PATH environment variable. In linux, the shared objects must be in a lib directory configured in the SO cache. With COM, a CLSID is used but that CLSID must be registered on the system, in which case the path to the file is specified in the registry, or a file can be referenced that is already in a PATH directory (though not recommended). That's just how everything works. The only assembly that doesn't show up in the references is mscorlib.dll, which is implied by the compiler unless overridden (possible in the VS.NET 2003 C# project properties, or using the command-line compiler since .NET 1.0 (v1.0.3705)).

          -----BEGIN GEEK CODE BLOCK----- Version: 3.21 GCS/G/MU d- s: a- C++++ UL@ P++(+++) L+(--) E--- W+++ N++ o+ K? w++++ O- M(+) V? PS-- PE Y++ PGP++ t++@ 5 X+++ R+@ tv+ b(-)>b++ DI++++ D+ G e++>+++ h---* r+++ y+++ -----END GEEK CODE BLOCK-----

          M 1 Reply Last reply
          0
          • M Mephisto187

            I want to use a dll without manually adding a reference in the solution explorer. Is there any way to do this ?? Something corresponding to the good old "import" maybe ???

            J Offline
            J Offline
            Jose Fco Bonnin
            wrote on last edited by
            #5

            You can use reflection to load an assembly. Assembly a = Assembly.LoadFrom("your.dll"); Type t = a.GetType("namespace.classname"); MethodInfo m = t.GetMethod("method"); object o = Activator.CreateInstance(t); object result = m.Invoke(o,new object[]{parameters}); If you want to avoid call methods in this way, you can also create an interface with all the necessary information about the dll and when you call CreateInstance just cast to it. For instance: public interface IAnInterface { void Method(); } ....... IAnInterface anInterface = (IAnInterface)o.CreateInstance(atype); IAnInterface.Method();

            J M 2 Replies Last reply
            0
            • J Jose Fco Bonnin

              You can use reflection to load an assembly. Assembly a = Assembly.LoadFrom("your.dll"); Type t = a.GetType("namespace.classname"); MethodInfo m = t.GetMethod("method"); object o = Activator.CreateInstance(t); object result = m.Invoke(o,new object[]{parameters}); If you want to avoid call methods in this way, you can also create an interface with all the necessary information about the dll and when you call CreateInstance just cast to it. For instance: public interface IAnInterface { void Method(); } ....... IAnInterface anInterface = (IAnInterface)o.CreateInstance(atype); IAnInterface.Method();

              J Offline
              J Offline
              Jose Fco Bonnin
              wrote on last edited by
              #6

              I type mismatched the last line should be "anInterface.Method();" instead of "IAnInterface.Method();" it was evident but .....

              1 Reply Last reply
              0
              • H Heath Stewart

                What do you mean, "The studio can create references to any registered component on the system."? Your project needs to reference the assemblies required by your project, period. When you add components, the assemblies in which that component and its dependencies are defined are added automatically if they aren't added already, but only when you use the designer. If you manually type the code, it does not do this automatically. It's all because of the designer. If you're talking about the assemblies that show up in the Add Reference dialog, you can add additional directories in which to search in the HKEY_LOCAL_MACHINE\Software\Microsoft\.NETFramework\Assembly folders key. If you don't want to worry about where assemblies are located (even though VS.NET will copy assemblies to the application directory that aren't installed in the GAC), you can use gacutil.exe to add assemblies to the Global Assembly Cache (GAC), or drag-n-drop the assemblies into the %WINDIR%\assembly directory. This is no different than any other language or platform, as I said before. In Java, the classes or JAR files have to be in the CLASSPATH when compiling or running the application (you can override the CLASSPATH on the command line, though). In Win32, libs must be linked in when compiling and the DLLs must either be in the application directory or in a directory in the PATH environment variable. In linux, the shared objects must be in a lib directory configured in the SO cache. With COM, a CLSID is used but that CLSID must be registered on the system, in which case the path to the file is specified in the registry, or a file can be referenced that is already in a PATH directory (though not recommended). That's just how everything works. The only assembly that doesn't show up in the references is mscorlib.dll, which is implied by the compiler unless overridden (possible in the VS.NET 2003 C# project properties, or using the command-line compiler since .NET 1.0 (v1.0.3705)).

                -----BEGIN GEEK CODE BLOCK----- Version: 3.21 GCS/G/MU d- s: a- C++++ UL@ P++(+++) L+(--) E--- W+++ N++ o+ K? w++++ O- M(+) V? PS-- PE Y++ PGP++ t++@ 5 X+++ R+@ tv+ b(-)>b++ DI++++ D+ G e++>+++ h---* r+++ y+++ -----END GEEK CODE BLOCK-----

                M Offline
                M Offline
                Mephisto187
                wrote on last edited by
                #7

                Ok, thank you for your detailed describtion !! In my case it´s a com dll I want to load by manually typeing the code (that´s why I was talking about registered components). The reason is that I don´t want that people, using my code in their own projects, have to care about the references I need. I want to enable my code to collect everything it needs on it´s own.

                H 1 Reply Last reply
                0
                • J Jose Fco Bonnin

                  You can use reflection to load an assembly. Assembly a = Assembly.LoadFrom("your.dll"); Type t = a.GetType("namespace.classname"); MethodInfo m = t.GetMethod("method"); object o = Activator.CreateInstance(t); object result = m.Invoke(o,new object[]{parameters}); If you want to avoid call methods in this way, you can also create an interface with all the necessary information about the dll and when you call CreateInstance just cast to it. For instance: public interface IAnInterface { void Method(); } ....... IAnInterface anInterface = (IAnInterface)o.CreateInstance(atype); IAnInterface.Method();

                  M Offline
                  M Offline
                  Mephisto187
                  wrote on last edited by
                  #8

                  In fact I´ve already tried it the first way, but didn´t know how to call methods on my created object. Simply connecting an interface pointer to my object never worked... The second way seems to be pretty smart, so I will try something like that. Thanks for your inspiration !!!

                  H 1 Reply Last reply
                  0
                  • M Mephisto187

                    I want to use a dll without manually adding a reference in the solution explorer. Is there any way to do this ?? Something corresponding to the good old "import" maybe ???

                    P Offline
                    P Offline
                    Paul Evans
                    wrote on last edited by
                    #9

                    I see you've told others they kind of missed the point, maybe you were looking for this? Perhaps what you are looking for is a dynamically loaded dll, as in a plug-in type senario. The author of the excellent DotNetMagic library has written this article that may turn a lightbulb on in your head: http://www.divil.co.uk/net/articles/plugins/plugins.asp /********************************** Paul Evans, Dorset, UK. Personal Homepage "EnjoySoftware" @ http://www.enjoysoftware.co.uk/ **********************************/

                    1 Reply Last reply
                    0
                    • M Mephisto187

                      In fact I´ve already tried it the first way, but didn´t know how to call methods on my created object. Simply connecting an interface pointer to my object never worked... The second way seems to be pretty smart, so I will try something like that. Thanks for your inspiration !!!

                      H Offline
                      H Offline
                      Heath Stewart
                      wrote on last edited by
                      #10

                      You cast the object that you get from Activator.CreateInstance to whatever class or interface it is. The assembly still has to be resolvable by either being in the application directory, a private path, the Global Assembly Cache, or by an implementation in your handler for the AppDomain.AssemblyResolve event. If the assembly can't be found that contains the Type, you'll get a TypeLoadException. Note that there is a significant performance issue using this over direct instantiation.

                      -----BEGIN GEEK CODE BLOCK----- Version: 3.21 GCS/G/MU d- s: a- C++++ UL@ P++(+++) L+(--) E--- W+++ N++ o+ K? w++++ O- M(+) V? PS-- PE Y++ PGP++ t++@ 5 X+++ R+@ tv+ b(-)>b++ DI++++ D+ G e++>+++ h---* r+++ y+++ -----END GEEK CODE BLOCK-----

                      1 Reply Last reply
                      0
                      • M Mephisto187

                        Ok, thank you for your detailed describtion !! In my case it´s a com dll I want to load by manually typeing the code (that´s why I was talking about registered components). The reason is that I don´t want that people, using my code in their own projects, have to care about the references I need. I want to enable my code to collect everything it needs on it´s own.

                        H Offline
                        H Offline
                        Heath Stewart
                        wrote on last edited by
                        #11

                        Sure you can redefine the COM interfaces you require in your source, but you have to worry about instantiating, casting, marshaling, and more. Using tlbimp.exe or aximp.exe to create a Runtime Callable Wrapper (RCW), or interop assembly, is much easier. Besides, whether or not other developers use that interop assembly is moot - they can just as easily create their own! Remember, an RCW isn't the COM object - thus containing the code - itself, but just a wrapper. So long as that COM object is installed on the target machine, anyone can create an interop assembly from it. And there's will be no different from yours except, perhaps, differing only by the public key token if you sign the interop assembly when you create it. Depending on the complexities of your COM object, manually defining all this stuff can be tedious, if not a waste of time.

                        -----BEGIN GEEK CODE BLOCK----- Version: 3.21 GCS/G/MU d- s: a- C++++ UL@ P++(+++) L+(--) E--- W+++ N++ o+ K? w++++ O- M(+) V? PS-- PE Y++ PGP++ t++@ 5 X+++ R+@ tv+ b(-)>b++ DI++++ D+ G e++>+++ h---* r+++ y+++ -----END GEEK CODE BLOCK-----

                        M 1 Reply Last reply
                        0
                        • H Heath Stewart

                          Sure you can redefine the COM interfaces you require in your source, but you have to worry about instantiating, casting, marshaling, and more. Using tlbimp.exe or aximp.exe to create a Runtime Callable Wrapper (RCW), or interop assembly, is much easier. Besides, whether or not other developers use that interop assembly is moot - they can just as easily create their own! Remember, an RCW isn't the COM object - thus containing the code - itself, but just a wrapper. So long as that COM object is installed on the target machine, anyone can create an interop assembly from it. And there's will be no different from yours except, perhaps, differing only by the public key token if you sign the interop assembly when you create it. Depending on the complexities of your COM object, manually defining all this stuff can be tedious, if not a waste of time.

                          -----BEGIN GEEK CODE BLOCK----- Version: 3.21 GCS/G/MU d- s: a- C++++ UL@ P++(+++) L+(--) E--- W+++ N++ o+ K? w++++ O- M(+) V? PS-- PE Y++ PGP++ t++@ 5 X+++ R+@ tv+ b(-)>b++ DI++++ D+ G e++>+++ h---* r+++ y+++ -----END GEEK CODE BLOCK-----

                          M Offline
                          M Offline
                          Mephisto187
                          wrote on last edited by
                          #12

                          Finally it works by invoking the methods on the object, created through the assembly, but I guess you are right. Compared with the two short mouse clicks you need to add a reference it´s really a waste of time. And using the rcw wrapper class is simply much more comfortable. Hmm.. so everything will remain unaffected, just explored a little more of the .new world :) Thanks to all of you for your help...

                          1 Reply Last reply
                          0
                          Reply
                          • Reply as topic
                          Log in to reply
                          • Oldest to Newest
                          • Newest to Oldest
                          • Most Votes


                          • Login

                          • Don't have an account? Register

                          • Login or register to search.
                          • First post
                            Last post
                          0
                          • Categories
                          • Recent
                          • Tags
                          • Popular
                          • World
                          • Users
                          • Groups