Groups | Blog | Home
all groups > dotnet faqs > september 2005 >

dotnet faqs : Create DLL, import in main application


CT
9/23/2005 12:00:00 AM
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
9/23/2005 12:00:00 AM
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]
CT
9/23/2005 12:00:00 AM
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
9/23/2005 12:00:00 AM
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
9/23/2005 12:00:00 AM
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
9/23/2005 12:00:00 AM
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]
Andy
9/23/2005 12:00:00 AM
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]
CT
9/23/2005 12:00:00 AM
[quoted text, click to view]
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
9/23/2005 8:32:11 AM
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,
CT
9/23/2005 9:01:33 AM
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
9/23/2005 9:40:36 AM
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
9/23/2005 10:36:38 AM
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
9/23/2005 10:59:56 AM
Looks good CT, thank you very much!

Andy


[quoted text, click to view]
Andy
9/23/2005 11:09:27 AM
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]
Richard Grimes [MVP]
10/15/2005 4:09:49 PM
[quoted text, click to view]

You'll find one here :-)

http://www.grimes.demon.co.uk/workshops/fusionWS.htm

Richard
--
www.grimes.demon.co.uk

AddThis Social Bookmark Button