minor reflection loadfrom/loadfile quirk

P

Peted

can anyone help me with the following

bellow is a routine i use that works, that will load an assembly from
a dll class from the HDD, and instantiate a form to be used to
control a device.

What is listed bellow works but

1 . before i got it to work this line

// Dynamically load the selected assembly.
deviceInterfaceAssembly = Assembly.LoadFile(devDir +
devicefile);

i was using .LoadFrom(devDir + devicefile);


can anyone tell me why the loadFrom did not work properly. It worked
for the first file location path i sent into the subroutine, after the
prog first runs, but every subequent file/location i tried to load,
the new file location is correctly passed into the routine, BUT using
loadfrom would only ever instantiate/load the first one loaded in the
program running session. It would not load anything other that the
first one passed in.

As i said what i have bellow works, but didnt LOadFrom work ?









private void loadDeviceInterfaceToSerialPort(int port,String
devicefile)
{

Assembly deviceInterfaceAssembly = null;


// Dynamically load the selected assembly.
deviceInterfaceAssembly = Assembly.LoadFile(devDir +
devicefile);

//
tbIPLinkFeedback.AppendText(deviceInterfaceAssembly.Location +
"\r\n");

// tbIPLinkFeedback.AppendText(devDir + devicefile +
"\r\n");

if (deviceInterfaceAssembly == null)
MessageBox.Show("not loaded");

// Get all types in assembly.
//ClassLibrary1.Form1
Type[] deviceInterfaceType =
deviceInterfaceAssembly.GetTypes();
//
tbIPLinkFeedback.AppendText(deviceInterfaceType[0].FullName + "\r\n");


//
tbIPLinkFeedback.AppendText(deviceInterfaceType[0].FullName);
//
tbIPLinkFeedback.AppendText(deviceInterfaceType[0].Name);

// Use late binding to create the type.

// tbIPLinkFeedback.AppendText(testform.Name);

ipLink.SetSerialPortDeviceInterface(port,
(Form)deviceInterfaceAssembly.CreateInstance(deviceInterfaceType[0].FullName));

ipLink.GetSerialPortDeviceInterface(port).MdiParent =
this;
ipLink.GetSerialPortDeviceInterface(port).StartPosition =
FormStartPosition.Manual;
ipLink.GetSerialPortDeviceInterface(port).FormBorderStyle =
FormBorderStyle.None;



ipLink.GetSerialPortDeviceInterface(port).Size = new
Size(400, this.ClientRectangle.Height);
ipLink.GetSerialPortDeviceInterface(port).Location = new
Point(540, 0);

ipLink.GetSerialPortDeviceInterface(port).Show();


}
 
M

Mike Labosh

can anyone tell me why the loadFrom did not work properly. It worked
for the first file location path i sent into the subroutine, after the
prog first runs, but every subequent file/location i tried to load,
the new file location is correctly passed into the routine, BUT using
loadfrom would only ever instantiate/load the first one loaded in the
program running session. It would not load anything other that the
first one passed in.

You have to (1) LoadFrom a library, and then (2) create an instance of a
namespace.classname. In the snippet below, the program has hit a table in a
database that contains library paths and class names, as well as other
stuff. You will want to focus on the line where I call Assembly.LoadFrom()
and watchAssembly.CreateInstance()

Apologies for this snippet being in VB.NET, but this is real production code
I wrote a couple years ago that does what you are asking. Where it declares
the watchAssembly variable with brackets around it, is because VB.NET has an
Assembly keyword so I had to qualify it with brackets. Then all those calls
to CType() is VB.NET's way of type casting. Post any questions about the
code below. The single quote introduces a comment, and the underscore means
line continuation. Also, C# does not have the "With" keyword, where, in
VB.NET, it resolves the object reference at the top of the With Block just
once, and then implies it for the contained property / method calls.

Public Function CreateWatcher(ByVal dataRow As DataRow) As FileWatcher

'=======================================================================
' Here's the fun part. If we get the AssemblyName and ClassName for a
' FileWatcher out of the database, we can use reflection to create the
' runtime instances. This way, future new FileWatcher subclasses can be
' written in their own assemblies and listed in the FileWatcher table.
' Then, to make them active, all you have to do is to restart this
' service. No need to disturb the code.

Dim watchAssembly As [Assembly]
Dim watchInstance As FileWatcher

Try
' Assembly.LoadFrom("c:\thisdir\thisassembly.dll")
watchAssembly = [Assembly].LoadFrom(dataRow("AssemblyName").ToString())

' Assembly.CreateInstance("ThisNamespace.ThisClass") then CType it
watchInstance = CType( _
watchAssembly.CreateInstance(dataRow("ClassName").ToString()) _
, FileWatcher)

' Anything that inherits from FileWatcher has these properties
With watchInstance
.ArchiveAction = CType(dataRow("ArchiveAction"), ArchiveActions)
.ArchivePath = dataRow("ArchivePath").ToString()
.BatchSize = CType(dataRow("BatchSize"), Integer)
.FilenameRegEx = dataRow("FilenameRegEx").ToString()
.Logger = log
.MonitorPath = dataRow("MonitorPath").ToString()
'.Enabled = False
End With

Return watchInstance

Catch exc As Exception
log.WriteEntry("Failed to create watcher using reflection: " &
exc.ToString(), EventLogEntryType.Error)
End Try

End Function

--

Peace & happy computing,

Mike Labosh, MCSD MCT
Owner, vbSensei.Com

"Escriba coda ergo sum." -- vbSensei
 
P

Peted

Ok, i pretty sure im doing all that correctly, as per my code bellow.

You see what i have bellow does work, but before i got to this stage i
was using assembley.loadfrom, instead of assembley.loadfile.

The problem i had with assembley.loadfrom, is that everytime i tried
to instantiate an instance of a new loaded dll and class, the program
would always revert to instantiate the class that was first opened
when the program runs. I dont get any errors, it just would only ever
instantiate the very first dll that was loaded when the program was
run, and i cant see any reason for this

when i changed assembley.loadfrom to assembly.loadfile, everything
worked fine, but i dont understand why. i cant see any problem in the
code that casued problems to use assembly.loadfrom ???

anyone have any ideas ?

Peted




private void loadDeviceInterfaceToSerialPort(int port,String
devicefile)
{

Assembly deviceInterfaceAssembly = null;


// Dynamically load the selected assembly.
deviceInterfaceAssembly = Assembly.LoadFile(devDir +
devicefile);

if (deviceInterfaceAssembly == null)
MessageBox.Show("not loaded");

Type[] deviceInterfaceType =
deviceInterfaceAssembly.GetTypes();

ipLink.SetSerialPortDeviceInterface(port,
(Form)deviceInterfaceAssembly.CreateInstance(deviceInterfaceType[0].FullName));

ipLink.GetSerialPortDeviceInterface(port).MdiParent =
this;
ipLink.GetSerialPortDeviceInterface(port).StartPosition =
FormStartPosition.Manual;
ipLink.GetSerialPortDeviceInterface(port).FormBorderStyle =
FormBorderStyle.None;



ipLink.GetSerialPortDeviceInterface(port).Size = new
Size(400, this.ClientRectangle.Height);
ipLink.GetSerialPortDeviceInterface(port).Location = new
Point(540, 0);

ipLink.GetSerialPortDeviceInterface(port).Show();


}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top