idos


Hi all,

I want to make part of my application to be created on runtime. So I figured that I can use reflection here. But know I am confuse, whether I should use Interface or Attribute to make sure the class created has certain functionality.

First option to use Interface:

In my application I specify interface
interface ICool
{
}

then the pluggable part could use the following code
class MyPluggableClass : ICool
{
}

then in my application I could specify-->
if ( typeToBeCreated is ICool == false) throw exception
else generate(typeToBeCreated)

Second option to use attribute

I specify the following attribute
class CoolAttribute : Attribute
{
}


then the pluggable part could use the following code
[Cool]
class MyPluggableClass
{
}

ten in my application I could specify -->
#get all attribute of typeToBeCreated
#if it contains CoolAttribute
then create
#else throw exception

Which way is the better way


Thank you in advace,
-idos-



Re: Attribute Vs. Interface

Mattias Sjogren


The .NET design guidelines favors attributes over marker interfaces. http://msdn2.microsoft.com/en-us/library/ms229022.aspx

A third option you can consider is to use an assembly level attribute to directly point out the type to instantiate. That way you don't have to load and look at every type (using Assembly.GetTypes or something like that which I assume you do now).






Re: Attribute Vs. Interface

idos

ic. thank you....






Re: Attribute Vs. Interface

Peter Ritchie

If you're looking for a sample to implement the attribute guideline, see http://forums.microsoft.com/MSDN/ShowPost.aspx PostID=2015677&SiteID=1