473,225 Members | 1,322 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,225 software developers and data experts.

Question about reflection

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... How do I make sure the .dll the users select are actually
plugins? I'm currently enforcing a policy where the concrete plugins need to
have a certain string somewhere in the classe's name, but that is not at all
a good idea...
What do you guys propose? What is the best way to ensure that this is
actually a plugin with the functions my applications expects the plugins to
have?

If it helps, I'm developing this in VB.Net using .Net 2.0.

Thanks in advance!

André Nogueira
Feb 17 '06 #1
4 1149
Hello, Andre!

If the assemly has the type ( interface ) your application is expecting then it is the plugin your app supports, if there is no such type in the assembly then it is not plugin.

--
Regards, Vadym Stetsyak
www: http://vadmyst.blogspot.com
Feb 17 '06 #2
Hi,

Thanks for the reply! I'm now using this code:

For Each t As Type In Plugin.GetTypes

If t.ToString.Contains("<my base plugin's base class>") Then

IsPlugin = True

End If

Next

It's not perfect (people can create a class which has the same class name,
and it would be identified as a plugin, but it's acceptable, I guess.

One more question though...

I have this code to load the plugin:

Dim Plugin As System.Reflection.Assembly = Nothing

Plugin = Reflection.Assembly.LoadFrom(FileName)

Dim typeObj As Type = Plugin.GetType(TypeName, True, True)

Dim instance As Object = Activator.CreateInstance(typeObj)

But... How do I know what the string "TypeName" should hold? Right now I'm
using a fixed name in the test plugins I'm creating, but people should be
able to name their classes anything they want... Is there any way to
overcome this, and still load the plugin - now knowing in advance what the
class name is?

Thanks again!

Andre Nogueira

"Vadym Stetsyak" <va*****@ukr.net> wrote in message
news:OK**************@TK2MSFTNGP12.phx.gbl...
Hello, Andre!

If the assemly has the type ( interface ) your application is expecting
then it is the plugin your app supports, if there is no such type in the
assembly then it is not plugin.

--
Regards, Vadym Stetsyak
www: http://vadmyst.blogspot.com

Feb 17 '06 #3
Hi Vadym

use the the overlaoded method of create instance which deal with types:
Dim Plugin As System.Reflection.Assembly = Nothing
Plugin = Reflection.Assembly.LoadFrom(FileName)
Dim typeObj As Type = Plugin.GetType(TypeName, True, True)
Dim instance As Object =
Activator.CreateInstance(GetType(<YourInterface>))

Regards,

Marcus
Andre Nogueira schrieb:
Hi,

Thanks for the reply! I'm now using this code:

For Each t As Type In Plugin.GetTypes

If t.ToString.Contains("<my base plugin's base class>") Then

IsPlugin = True

End If

Next

It's not perfect (people can create a class which has the same class name,
and it would be identified as a plugin, but it's acceptable, I guess.

One more question though...

I have this code to load the plugin:

Dim Plugin As System.Reflection.Assembly = Nothing

Plugin = Reflection.Assembly.LoadFrom(FileName)

Dim typeObj As Type = Plugin.GetType(TypeName, True, True)

Dim instance As Object = Activator.CreateInstance(typeObj)

But... How do I know what the string "TypeName" should hold? Right now I'm
using a fixed name in the test plugins I'm creating, but people should be
able to name their classes anything they want... Is there any way to
overcome this, and still load the plugin - now knowing in advance what the
class name is?

Thanks again!

Andre Nogueira

"Vadym Stetsyak" <va*****@ukr.net> wrote in message
news:OK**************@TK2MSFTNGP12.phx.gbl...
Hello, Andre!

If the assemly has the type ( interface ) your application is expecting
then it is the plugin your app supports, if there is no such type in the
assembly then it is not plugin.

--
Regards, Vadym Stetsyak
www: http://vadmyst.blogspot.com


Feb 17 '06 #4
Andre Nogueira wrote:
For Each t As Type In Plugin.GetTypes
You really should use Assembly.GetExportedTypes, not
Assembly.GetTypes. GetTypes returns all types - including private and
internal types that you can't access (without special permissions) -
while GetExportedTypes only returns public types. Smaller array, and
no special permissions required.
If t.ToString.Contains("<my base plugin's base class>") Then


Yuck! You really want to use Type.IsAssignableFrom, not string tests!

typeof(MyAbstractBaseClass).IsAssignableFrom(t)

(though it's probably better to use an interface than an abstract base
class.)

--
<http://www.midnightbeach.com>
Feb 18 '06 #5

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

Similar topics

2
by: Tony Tullemans | last post by:
I am trying to write a subroutine that will examine all the properties of a form to determine which of those are SqlCommand objects, and then set the Connection property of those SqlCommands to a...
1
by: Mike Malter | last post by:
I am just starting to work with reflection and I want to create a log that saves relevant information if a method call fails so I can call that method again later using reflection. I am...
2
by: Dan | last post by:
Let's say I have a class like: class Dummy { public const string CONE = "one"; public const string CTWO = "two"; ... other stuff .... }
7
by: John | last post by:
I have a class the reads in a file and sets the values of the file into its properties. This class is used to populate the data onto a form. This form has controls created at runtime based on...
4
by: Bob | last post by:
If you rename a type at design time, the the task list shows what code broke, which happens to be a very nice list of dependant classes and methods. Is there any way to produce such dependency...
4
by: Doug Handler | last post by:
Hi, I've developed a plug-in application that basically is constructed of 2 pieces 1). the "main framework" 2). channels. Using Reflection in the Main Framework, i dynamically load Channels. ...
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...
1
by: Tony Johansson | last post by:
Hello! This is a question about Reflection. I have a small program below where the class are called Mymemberinfo. This class Mymemberinfo is used in GetMembers to get all the member for this...
5
by: =?Utf-8?B?Q2hyaXN0aWFuIEhhdmVs?= | last post by:
Hi, is it faster to access the fields from an object directly by using reflection than accessing them by their properties using reflection? Thanks Christian
1
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
0
by: veera ravala | last post by:
ServiceNow is a powerful cloud-based platform that offers a wide range of services to help organizations manage their workflows, operations, and IT services more efficiently. At its core, ServiceNow...
0
by: VivesProcSPL | last post by:
Obviously, one of the original purposes of SQL is to make data query processing easy. The language uses many English-like terms and syntax in an effort to make it easy to learn, particularly for...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....

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.