473,395 Members | 1,941 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

minor reflection loadfrom/loadfile quirk

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(deviceInterfaceAssembl y.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(devic eInterfaceType[0].FullName));

ipLink.GetSerialPortDeviceInterface(port).MdiParen t =
this;
ipLink.GetSerialPortDeviceInterface(port).StartPos ition =
FormStartPosition.Manual;
ipLink.GetSerialPortDeviceInterface(port).FormBord erStyle =
FormBorderStyle.None;

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

ipLink.GetSerialPortDeviceInterface(port).Show();
}
Feb 17 '07 #1
2 2524
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
Feb 17 '07 #2
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(devic eInterfaceType[0].FullName));

ipLink.GetSerialPortDeviceInterface(port).MdiParen t =
this;
ipLink.GetSerialPortDeviceInterface(port).StartPos ition =
FormStartPosition.Manual;
ipLink.GetSerialPortDeviceInterface(port).FormBord erStyle =
FormBorderStyle.None;

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

ipLink.GetSerialPortDeviceInterface(port).Show();
}
Feb 18 '07 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Jozsef Bekes | last post by:
Hi All, I am trying to use reflection for getting all the classes from the dlls of a software written by my company. I am using this line: Assembly.LoadFile(s).GetTypes() Gettypes fails, I...
2
by: forvino | last post by:
Hi Geeks, Ihave a doubt in dotNet reflection. I m developing a tool which will returns set of public(access specifier) methods of the selected assembly. this works completely fine,...
11
by: Michael Maes | last post by:
Hello, I want to be able to load an assembly by selecting a dll from an OpenFileDialog. This seems to work, exept when that assembly references "3rd-Party dll's". Just this simple line: Dim...
2
by: Jeff | last post by:
I am trying to dynamically load an assembly via reflection and then invoke a method of that assembly that will populate a custom type collection passed into the method byref. I am able to...
4
by: Andre Nogueira | last post by:
Hi guys I am developing a plugin-enabled application, and for that I am using reflection. I created an abstract ("MustInherit") class, from which all plugins must inherit. My question is......
3
by: John Wright | last post by:
How can I set the property of a loaded Assembly using reflection. My Shell program will log in a person and retrieve a list of all programs the person can use. When the shell program launches a...
4
by: Chris | last post by:
I am looking at this code in C# Assembly a=Assembly.LoadFrom("employee.dll"); Type t=a.GetType("Company.Employee"); I can't figure out what it is in VB.net can anyone help. I try Dim...
1
by: DeveloperX | last post by:
I've knocked up a quick plug in project (actually it's 6 projects, but more on that in a moment). This project is referenced by a shared add in (used for Excel). The issue I have is that when...
3
by: binder.christian | last post by:
Hi! I have two applications: * mainapp.exe * plugin01.exe I have 2 "friend" variables in mainapp.exe: Friend gblConn as SqlConnection Friend gblPath as string
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.