all groups > dotnet faqs > september 2005 >
dotnet faqs :
Create DLL, import in main application
Hi Andy, Dim form1Type As Type = _ sampleModule.GetType("ClassLibrary.Module1") Dim fi As FieldInfo = form1Type.GetField("Version") MessageBox.Show(CStr(fi.Name)) Please note that the fields can't be private. -- Carsten Thomsen Enterprise Development with VS .NET, UML, AND MSF http://www.apress.com/book/bookDisplay.html?bID=105 Communities - http://community.integratedsolutions.dk [quoted text, click to view] "Andy" <andy.mail@zwickau-net.de> wrote in message news:eLLqH5BwFHA.720@TK2MSFTNGP15.phx.gbl... > CT, that works really fine. Thank you very much! > > One question left: in my module I would like to insert some constants like > "Version", "Company" and so on and call these constants from my main > application. How to realize that? > > Thanks for your patience, > Andy > > > > CT wrote: >> If you want to load Windows Forms from your assemblies, here's one way of >> doing it: >> >> Dim assemblyToLoad As [Assembly] >> ' Load assembly from file (enter the correct path and assembly >> name) >> assemblyToLoad = >> [Assembly].LoadFrom("D:\Projects\ClassLibrary\bin\ClassLibrary.dll") >> >> ' Find the ClassLibrary.dll module >> Dim classLibraryModule As [Module] = >> assemblyToLoad.GetModule("ClassLibrary.dll") >> >> ' Find the Form1 type >> ' Notice how it is the full name that is used, i.e. type name is >> prefixed with namespace >> Dim form1Type As Type = _ >> classLibraryModule.GetType("ClassLibrary.Form1") >> >> ' Create instance of form and display >> Dim assemblyForm As Form = >> CType(Activator.CreateInstance(form1Type), Form) >> assemblyForm.Show() >> >> I hope that helps :-) >>
Sorry for nerving but fi.Name only returns the name of the field but not the value. How can i return the value (it´s a string variable)... Thanks again, Andy [quoted text, click to view] > Hi Andy, > > Dim form1Type As Type = _ > sampleModule.GetType("ClassLibrary.Module1") > > Dim fi As FieldInfo = form1Type.GetField("Version") > MessageBox.Show(CStr(fi.Name)) > > Please note that the fields can't be private.
This is one way: fi.GetValue(fi).ToString Feel free to use a conversion function instead of the ToString method, depedning on the type of constant you have, i.e. CInt for and Integer etc: Dim x As Integer = CInt(fi.GetValue(fi)) -- Carsten Thomsen Enterprise Development with VS .NET, UML, AND MSF http://www.apress.com/book/bookDisplay.html?bID=105 Communities - http://community.integratedsolutions.dk [quoted text, click to view] "Andy" <andy.mail@zwickau-net.de> wrote in message news:%23nitqDDwFHA.256@TK2MSFTNGP15.phx.gbl... > Sorry for nerving but fi.Name only returns the name of the field but not > the value. How can i return the value (it´s a string variable)... > > Thanks again, > Andy > > >> Hi Andy, >> >> Dim form1Type As Type = _ >> sampleModule.GetType("ClassLibrary.Module1") >> >> Dim fi As FieldInfo = form1Type.GetField("Version") >> MessageBox.Show(CStr(fi.Name)) >> >> Please note that the fields can't be private. >>
That´s what i´ve tried but it ends up with an exception that the object type cant be converted to the target type. Displaying the name of the constant is no problem... My code in the module: Public Const Company As String = "TestCompany" My code in the main application: Dim PublicField As FieldInfo PublicField = FormType.GetField("Company") MsgBox(PublicField.Name.ToString) <- that works perfectly, displaying "Company" MsgBox(PublicField.GetValue(PublicField).ToString) <- no way :-( Any idea? Thanks, Andy [quoted text, click to view] CT wrote: > This is one way: > > fi.GetValue(fi).ToString > > Feel free to use a conversion function instead of the ToString method, > depedning on the type of constant you have, i.e. CInt for and Integer etc: > > Dim x As Integer = CInt(fi.GetValue(fi))
Hmm, this works for me: Dim assemblyToLoad As [Assembly] ' Load assembly from file assemblyToLoad = [Assembly].LoadFrom("D:\Projects\ClassLibrary\bin\ClassLibrary.dll") ' Find the assembly module Dim assemblyModule As [Module] = assemblyToLoad.GetModule("ClassLibrary.dll") ' Find the Module type. Dim moduleType As Type = _ assemblyModule.GetType("ClassLibrary.Module1") ' Get the public Company constant Dim PublicField As FieldInfo PublicField = moduleType.GetField("Company") MsgBox(PublicField.Name.ToString) MsgBox(PublicField.GetValue(PublicField).ToString) -- Carsten Thomsen Enterprise Development with VS .NET, UML, AND MSF http://www.apress.com/book/bookDisplay.html?bID=105 Communities - http://community.integratedsolutions.dk [quoted text, click to view] "Andy" <andy.mail@zwickau-net.de> wrote in message news:uM11scEwFHA.3644@TK2MSFTNGP11.phx.gbl... > That´s what i´ve tried but it ends up with an exception that the object > type cant be converted to the target type. > Displaying the name of the constant is no problem... > > My code in the module: > Public Const Company As String = "TestCompany" > > My code in the main application: > Dim PublicField As FieldInfo > PublicField = FormType.GetField("Company") > MsgBox(PublicField.Name.ToString) <- that works perfectly, > displaying "Company" > MsgBox(PublicField.GetValue(PublicField).ToString) <- no way :-( > > > Any idea? > > Thanks, > Andy > > > CT wrote: >> This is one way: >> >> fi.GetValue(fi).ToString >> >> Feel free to use a conversion function instead of the ToString method, >> depedning on the type of constant you have, i.e. CInt for and Integer >> etc: >> >> Dim x As Integer = CInt(fi.GetValue(fi)) >>
No way, it doesn't want to work here... Maybe you can send a copy of the main project and the library project to andy.mail@zwickau-net.de? Or can I call a function from the module library, which throws the variables i would like to have? Thank you, Andy [quoted text, click to view] CT wrote: > Hmm, this works for me: > > Dim assemblyToLoad As [Assembly] > ' Load assembly from file > assemblyToLoad = > [Assembly].LoadFrom("D:\Projects\ClassLibrary\bin\ClassLibrary.dll") > > ' Find the assembly module > Dim assemblyModule As [Module] = > assemblyToLoad.GetModule("ClassLibrary.dll") > ' Find the Module type. > Dim moduleType As Type = _ > assemblyModule.GetType("ClassLibrary.Module1") > > ' Get the public Company constant > Dim PublicField As FieldInfo > PublicField = moduleType.GetField("Company") > MsgBox(PublicField.Name.ToString) > MsgBox(PublicField.GetValue(PublicField).ToString) >
Oh no, my fault! I handled the wrong dll file, now it works. Thanks for all your patience, Carsten! I hope you´ll have a very nice and relaxed weekend! Best regards and thanks again, Andy [quoted text, click to view] Andy wrote: > No way, it doesn't want to work here... > Maybe you can send a copy of the main project and the library project to > andy.mail@zwickau-net.de? > > Or can I call a function from the module library, which throws the > variables i would like to have? > > Thank you, > Andy > > > CT wrote: >> Hmm, this works for me: >> >> Dim assemblyToLoad As [Assembly] >> ' Load assembly from file >> assemblyToLoad = >> [Assembly].LoadFrom("D:\Projects\ClassLibrary\bin\ClassLibrary.dll") >> >> ' Find the assembly module >> Dim assemblyModule As [Module] = >> assemblyToLoad.GetModule("ClassLibrary.dll") >> ' Find the Module type. >> Dim moduleType As Type = _ >> assemblyModule.GetType("ClassLibrary.Module1") >> >> ' Get the public Company constant >> Dim PublicField As FieldInfo >> PublicField = moduleType.GetField("Company") >> MsgBox(PublicField.Name.ToString) >> MsgBox(PublicField.GetValue(PublicField).ToString) >>
[quoted text, click to view] > Oh no, my fault! I handled the wrong dll file, now it works.
One of those days, eh? ;-) Enjoy the weekend. -- Carsten Thomsen Enterprise Development with VS .NET, UML, AND MSF http://www.apress.com/book/bookDisplay.html?bID=105 Communities - http://community.integratedsolutions.dk [quoted text, click to view] "Andy" <andy.mail@zwickau-net.de> wrote in message news:eKGpNJFwFHA.2312@TK2MSFTNGP14.phx.gbl... > Oh no, my fault! I handled the wrong dll file, now it works. > > Thanks for all your patience, Carsten! > > I hope you´ll have a very nice and relaxed weekend! > > Best regards and thanks again, > Andy > > > Andy wrote: >> No way, it doesn't want to work here... >> Maybe you can send a copy of the main project and the library project to >> andy.mail@zwickau-net.de? >> >> Or can I call a function from the module library, which throws the >> variables i would like to have? >> >> Thank you, >> Andy >> >> >> CT wrote: >>> Hmm, this works for me: >>> >>> Dim assemblyToLoad As [Assembly] >>> ' Load assembly from file >>> assemblyToLoad = >>> [Assembly].LoadFrom("D:\Projects\ClassLibrary\bin\ClassLibrary.dll") >>> >>> ' Find the assembly module >>> Dim assemblyModule As [Module] = >>> assemblyToLoad.GetModule("ClassLibrary.dll") >>> ' Find the Module type. >>> Dim moduleType As Type = _ >>> assemblyModule.GetType("ClassLibrary.Module1") >>> >>> ' Get the public Company constant >>> Dim PublicField As FieldInfo >>> PublicField = moduleType.GetField("Company") >>> MsgBox(PublicField.Name.ToString) >>> MsgBox(PublicField.GetValue(PublicField).ToString) >>> >>>
Hi! I have a main application here and developed 2 modules as DLL files, which will be updated from time to time. The main application ships without the modules so i want to be able, to put the module DLLs in application folder and call them from the main application. My question: is it possible to use my modules like that? The DLL files would have a determined format like company.modulename.dll and the start function could have the same name in all modules. Is there any way? If necessary I could write an XML file which contains all module names and the names of it dll files to load them into the main application. Please help! Thanks,
Andy, You're using .NET right? If so, look at the Assembly.Load method for this purpose. -- Carsten Thomsen Enterprise Development with VS .NET, UML, AND MSF http://www.apress.com/book/bookDisplay.html?bID=105 Communities - http://community.integratedsolutions.dk [quoted text, click to view] "Andy" <andy.mail@zwickau-net.de> wrote in message news:%23$sQPhAwFHA.1032@TK2MSFTNGP12.phx.gbl... > Hi! > > I have a main application here and developed 2 modules as DLL files, which > will be updated from time to time. > The main application ships without the modules so i want to be able, to > put the module DLLs in application folder and call them from the main > application. > > My question: is it possible to use my modules like that? The DLL files > would have a determined format like company.modulename.dll and the start > function could have the same name in all modules. Is there any way? > > If necessary I could write an XML file which contains all module names and > the names of it dll files to load them into the main application. > > Please help! > > Thanks, > Andy
CT, thanks for your very quick answer. Is it possible to include forms in assemblies so the assembly can be a complete program which is called from the main application? Can you recommend an assembly tutorial to me? Thanks, Andy [quoted text, click to view] CT wrote: > Andy, > > You're using .NET right? If so, look at the Assembly.Load method for this > purpose.
If you want to load Windows Forms from your assemblies, here's one way of doing it: Dim assemblyToLoad As [Assembly] ' Load assembly from file (enter the correct path and assembly name) assemblyToLoad = [Assembly].LoadFrom("D:\Projects\ClassLibrary\bin\ClassLibrary.dll") ' Find the ClassLibrary.dll module Dim classLibraryModule As [Module] = assemblyToLoad.GetModule("ClassLibrary.dll") ' Find the Form1 type ' Notice how it is the full name that is used, i.e. type name is prefixed with namespace Dim form1Type As Type = _ classLibraryModule.GetType("ClassLibrary.Form1") ' Create instance of form and display Dim assemblyForm As Form = CType(Activator.CreateInstance(form1Type), Form) assemblyForm.Show() I hope that helps :-) -- Carsten Thomsen Enterprise Development with VS .NET, UML, AND MSF http://www.apress.com/book/bookDisplay.html?bID=105 Communities - http://community.integratedsolutions.dk [quoted text, click to view] "Andy" <andy.mail@zwickau-net.de> wrote in message news:eGmHeHBwFHA.3000@TK2MSFTNGP12.phx.gbl... > CT, > > thanks for your very quick answer. > Is it possible to include forms in assemblies so the assembly can be a > complete program which is called from the main application? > > Can you recommend an assembly tutorial to me? > > Thanks, > Andy > > > CT wrote: >> Andy, >> >> You're using .NET right? If so, look at the Assembly.Load method for this >> purpose. >>
Looks good CT, thank you very much! Andy [quoted text, click to view] CT wrote: > If you want to load Windows Forms from your assemblies, here's one way of > doing it: > > Dim assemblyToLoad As [Assembly] > ' Load assembly from file (enter the correct path and assembly name) > assemblyToLoad = > [Assembly].LoadFrom("D:\Projects\ClassLibrary\bin\ClassLibrary.dll") > > ' Find the ClassLibrary.dll module > Dim classLibraryModule As [Module] = > assemblyToLoad.GetModule("ClassLibrary.dll") > > ' Find the Form1 type > ' Notice how it is the full name that is used, i.e. type name is > prefixed with namespace > Dim form1Type As Type = _ > classLibraryModule.GetType("ClassLibrary.Form1") > > ' Create instance of form and display > Dim assemblyForm As Form = CType(Activator.CreateInstance(form1Type), > Form) > assemblyForm.Show() > > I hope that helps :-)
CT, that works really fine. Thank you very much! One question left: in my module I would like to insert some constants like "Version", "Company" and so on and call these constants from my main application. How to realize that? Thanks for your patience, Andy [quoted text, click to view] CT wrote: > If you want to load Windows Forms from your assemblies, here's one way of > doing it: > > Dim assemblyToLoad As [Assembly] > ' Load assembly from file (enter the correct path and assembly name) > assemblyToLoad = > [Assembly].LoadFrom("D:\Projects\ClassLibrary\bin\ClassLibrary.dll") > > ' Find the ClassLibrary.dll module > Dim classLibraryModule As [Module] = > assemblyToLoad.GetModule("ClassLibrary.dll") > > ' Find the Form1 type > ' Notice how it is the full name that is used, i.e. type name is > prefixed with namespace > Dim form1Type As Type = _ > classLibraryModule.GetType("ClassLibrary.Form1") > > ' Create instance of form and display > Dim assemblyForm As Form = CType(Activator.CreateInstance(form1Type), > Form) > assemblyForm.Show() > > I hope that helps :-)
[quoted text, click to view] Andy wrote: > CT, > > thanks for your very quick answer. > Is it possible to include forms in assemblies so the assembly can be a > complete program which is called from the main application? > > Can you recommend an assembly tutorial to me?
You'll find one here :-) http://www.grimes.demon.co.uk/workshops/fusionWS.htm Richard -- www.grimes.demon.co.uk
Don't see what you're looking for? Try a search.
|
|
|