473,396 Members | 2,109 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,396 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 1153
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
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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: 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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
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...
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...

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.