473,756 Members | 6,106 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Reflection

BK
I've used reflection quite a bit to launch forms, it works great,
requires little coding, and allows me to launch forms from a dynamic
menu.

Now I have a need to instantiate any one of several business classes
dynamically, so my natural inclination was to use reflection. The
problem I'm running into is that my business classes require arguments
to be passed in where as the forms did not. Here is an example of
launching a form:

Dim ExternalAssembl y As System.Reflecti on.Assembly = _
System.Reflecti on.Assembly.Loa dFrom(Applicati on.StartupPath &
"\" & Container)
Dim CalledForm As BravoBaseForm = _
ExternalAssembl y.CreateInstanc e(FullName, True)
CalledForm.Show ()

Container, in my example, is an assembly containing the form,
typically an Exe or a Dll. Fullname refers to the class name
representing the form I want to launch. This code works fine for
launching a form. Now contrast that with the following code:

ExternalAssembl y = System.Reflecti on.Assembly.Loa dFrom(TargetHos t)
BizClass = Me.ExternalAsse mbly.CreateInst ance(ClassName, _
True, BindingFlags.Cr eateInstance, Nothing, args, _
System.Globaliz ation.CultureIn fo.CurrentCultu re, Nothing)

To pass in arguments, I have to use another overloaded CreateInstance
call. I'll be honest, I'm not sure what all of the arguments are, I'm
making somewhat educated guesses on them. The main ones I do
understand is the first argument which is the name of the class I want
to instantiate, and the fifth one which is an array of objects
representing the parameters required to instantiate the class.

The code in my second example executes without any errors, but
BizClass is equal to nothing afterwards. Any thoughts or comments are
appreciated.

Feb 7 '07 #1
7 2228
classes are a waste of time; they slow your programs down

80% of vb6 developers didn't need COM components

Feb 7 '07 #2
On Feb 7, 8:56 am, "BK" <bkunn...@hotma il.comwrote:
I've used reflection quite a bit to launch forms, it works great,
requires little coding, and allows me to launch forms from a dynamic
menu.

Now I have a need to instantiate any one of several business classes
dynamically, so my natural inclination was to use reflection. The
problem I'm running into is that my business classes require arguments
to be passed in where as the forms did not. Here is an example of
launching a form:

Dim ExternalAssembl y As System.Reflecti on.Assembly = _
System.Reflecti on.Assembly.Loa dFrom(Applicati on.StartupPath &
"\" & Container)
Dim CalledForm As BravoBaseForm = _
ExternalAssembl y.CreateInstanc e(FullName, True)
CalledForm.Show ()

Container, in my example, is an assembly containing the form,
typically an Exe or a Dll. Fullname refers to the class name
representing the form I want to launch. This code works fine for
launching a form. Now contrast that with the following code:

ExternalAssembl y = System.Reflecti on.Assembly.Loa dFrom(TargetHos t)
BizClass = Me.ExternalAsse mbly.CreateInst ance(ClassName, _
True, BindingFlags.Cr eateInstance, Nothing, args, _
System.Globaliz ation.CultureIn fo.CurrentCultu re, Nothing)

To pass in arguments, I have to use another overloaded CreateInstance
call. I'll be honest, I'm not sure what all of the arguments are, I'm
making somewhat educated guesses on them. The main ones I do
understand is the first argument which is the name of the class I want
to instantiate, and the fifth one which is an array of objects
representing the parameters required to instantiate the class.

The code in my second example executes without any errors, but
BizClass is equal to nothing afterwards. Any thoughts or comments are
appreciated.
Hi,

Take a look at what I just posted in the following thread. I
presented two methods for creating a String object via reflection.
Each method required passing parameters to the constructor.

http://groups.google.com/group/micro...cc4c00915f1fd5

Brian

Feb 7 '07 #3
BK
On Feb 7, 11:45 am, "Brian Gideon" <briangid...@ya hoo.comwrote:
On Feb 7, 8:56 am, "BK" <bkunn...@hotma il.comwrote:
I've used reflection quite a bit to launch forms, it works great,
requires little coding, and allows me to launch forms from a dynamic
menu.
Now I have a need to instantiate any one of several business classes
dynamically, so my natural inclination was to use reflection. The
problem I'm running into is that my business classes require arguments
to be passed in where as the forms did not. Here is an example of
launching a form:
Dim ExternalAssembl y As System.Reflecti on.Assembly = _
System.Reflecti on.Assembly.Loa dFrom(Applicati on.StartupPath &
"\" & Container)
Dim CalledForm As BravoBaseForm = _
ExternalAssembl y.CreateInstanc e(FullName, True)
CalledForm.Show ()
Container, in my example, is an assembly containing the form,
typically an Exe or a Dll. Fullname refers to the class name
representing the form I want to launch. This code works fine for
launching a form. Now contrast that with the following code:
ExternalAssembl y = System.Reflecti on.Assembly.Loa dFrom(TargetHos t)
BizClass = Me.ExternalAsse mbly.CreateInst ance(ClassName, _
True, BindingFlags.Cr eateInstance, Nothing, args, _
System.Globaliz ation.CultureIn fo.CurrentCultu re, Nothing)
To pass in arguments, I have to use another overloaded CreateInstance
call. I'll be honest, I'm not sure what all of the arguments are, I'm
making somewhat educated guesses on them. The main ones I do
understand is the first argument which is the name of the class I want
to instantiate, and the fifth one which is an array of objects
representing the parameters required to instantiate the class.
The code in my second example executes without any errors, but
BizClass is equal to nothing afterwards. Any thoughts or comments are
appreciated.

Hi,

Take a look at what I just posted in the following thread. I
presented two methods for creating a String object via reflection.
Each method required passing parameters to the constructor.

http://groups.google.com/group/micro...languages.vb/b...

Brian
Thanks for the reply, however neither of your examples will work. The
line:

Dim typ As Type = GetType(String)

wouldn't work for me since my type is unknown at design time. Think
of it this way. Lets say I have 5 assemblies, all dll's, that hold
some unknown number of classes in them. Lets also assume for
simplicity sake that all of the classes in any of the dlls conform to
a known interface called iBusinessObject . Programmaticall y, I want to
instantiate any one of these classes but I won't know ahead of time
which one. Lets also say that the name of the class is known, but it
is a string variable. How would I get the type so I can instantiate
it?

Feb 7 '07 #4
On Feb 7, 2:39 pm, "BK" <bkunn...@hotma il.comwrote:
Thanks for the reply, however neither of your examples will work. The
line:

Dim typ As Type = GetType(String)

wouldn't work for me since my type is unknown at design time. Think
of it this way. Lets say I have 5 assemblies, all dll's, that hold
some unknown number of classes in them. Lets also assume for
simplicity sake that all of the classes in any of the dlls conform to
a known interface called iBusinessObject . Programmaticall y, I want to
instantiate any one of these classes but I won't know ahead of time
which one. Lets also say that the name of the class is known, but it
is a string variable. How would I get the type so I can instantiate
it?
Hi,

No problem. You can do this instead to load a type at runtime.

Dim typ as Type = Type.GetType("Y ourNamespace.Yo urType")

Or if the assembly your type is in isn't loaded yet do this.

Dim asm As Assembly = Assembly.Load(" YourAssembly")
Dim typ As Type = asm.GetType("Yo urNamespace.You rType")

Brian

Feb 7 '07 #5
BK
Thanks, but now I try this:

Dim typ As Type = ExternalAssembl y.GetType(FullC lassName)
Me.BizClass = DirectCast(Me.B izClass, typ)

and I get an error telling me "Type typ is not defined". Mind you if
I comment the second line of code out, it runs just fine.

?????

Feb 7 '07 #6
When you use DirectCast the second parameter is the object type, not an
instance Type. If you are trying to cast to various type names you may have
to come up with a better way since this way is for specific types. I use
DirectCast only when I know what type to cast to. I don't think it works
dynamically.

Lloyd Sheen
"BK" <bk******@hotma il.comwrote in message
news:11******** **************@ a75g2000cwd.goo glegroups.com.. .
Thanks, but now I try this:

Dim typ As Type = ExternalAssembl y.GetType(FullC lassName)
Me.BizClass = DirectCast(Me.B izClass, typ)

and I get an error telling me "Type typ is not defined". Mind you if
I comment the second line of code out, it runs just fine.

?????
Feb 7 '07 #7
"BK" <bk******@hotma il.comschrieb
Thanks, but now I try this:

Dim typ As Type = ExternalAssembl y.GetType(FullC lassName)
Me.BizClass = DirectCast(Me.B izClass, typ)

and I get an error telling me "Type typ is not defined". Mind you
if I comment the second line of code out, it runs just fine.

?????
Directcast requires a type name, not a type object. What you're trying is
contradictive:

If you specify a variable type, you already know the type of object that you
want to assign to the variable. Consequently you can write the same type
name when you use Directcast.

If you have a System.Type object, you do not know the type of the variable
when writing the code. As a consequence, you can not know how to declare the
variable - well, it's always "Object" at least.
Armin

Feb 7 '07 #8

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

Similar topics

0
1382
by: A. Wiebenga | last post by:
Hi all! I am a student at the Hogeschool van Arnhem en Nijmegen in Holland. I am currently involved in a research project regarding Reflection. Purpose of the research project is to document what Reflection is (I can answer this question myself), what kind of Reflection Techniques there are (.NET Reflection, Python Reflection etc. I can answer this one myself too). The main question I need to answer is related to the implementation of...
10
7367
by: Sunny | last post by:
Hi, I have an old problem which I couldn't solve so far. Now I have found a post in that group that gave me an idea, but I can not fully understand it. The problem is: I'm trying to use a Windows.Forms.UserControl in a COM environment, i.e. I want to host that control in a COM host. So far, so good, I can host it, but I can not reach the parent COM object from the control (Parent property is null :( ). I have stopped the control in the...
2
864
by: Jason Coyne Gaijin42 | last post by:
I have seen several people looking for a way to access the Columns collection when using the AutoGenerate = true option. Some people have gotten so far as to find the private autoGenColumnsArray that has the information, but we as developers have no way to access this information. I have come up with a solution for the problem, (as I am sure many others have) using reflection. Here is some sample code that will print out the auto...
2
2012
by: Mark | last post by:
Am I out of my mind if I use Reflection everytime someone logs into our site to get and track the current Major/Minor/Build/Revision version that the person is viewing our site through? This information would then be logged to a database along with some other information about the user. Thanks in advance. Mark
0
1548
by: Shawn Hogan | last post by:
Hi everyone, I've been trying to execute a control's private event code via reflection from another class with the goal of potentially doing some unit testing. The examples below are trying to execute button2's click event. This works great when i know the name of the method that i want to invoke. I do so by doing this: Dim AssemblyPointer As Reflection.Assembly
3
2329
by: HL | last post by:
The requirement is to send some information to other objects. The objects to whom the information has to be sent is not available at compile time. The names of the types (objects) will be provided through an external file in which case we can use reflection to create objects of that type at run time. Case 1: The methods of those types can be invoked through reflection. Case 2: The caller can publish certain events. The types (objects)...
9
3848
by: Kuberan Naganathan | last post by:
Hello all Does anyone know of a good way to use reflection in c++? I don't mean simply using rtti or dynamic casting. I'm talking about java/c# style reflection where an actual instance of an object can be constructed through reflection and method calls can be made via the reflection apis.
17
2309
by: raylopez99 | last post by:
What good is C# Reflection, other than to find out what types are in an assembly? And to dynamically invoke methods in an assembly (.dll or .exe)? Also, bonus question, can you use Reflection to build a compiler? One that will construct a user defined class "on the fly" (literally, the user defines a class, instantiates it, and runs it from the console mode, all the while prompted by the program)? I guess so, but my final question...
0
1700
by: Gustavo Arriola | last post by:
Hola a todos! Estoy intentando ejecutar un método usando Reflection. El código es el siguiente: public static void SoapHandler(Exception Error) { try { Type assemblyType;
0
9271
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9868
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9836
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9707
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7242
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6533
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5301
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3804
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3352
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.