1

Created MetadataAttribute that allows using multiple.

[MetadataAttribute]
[AttributeUsage(AttributeTargets.Method, AllowMultiple = true)]
public class BusinessLogicMetaDataAttribute : ExportAttribute, IBusinessLogicMetaData
{
     //......
}

then I am using GetExports<T>() to import methods.

//.....
var imported = _container.GetExports<Action<object, EvantArgs>, IBusinessLogicMetaData>("myplugin");
//.....

Here is my plugin method:

[BusinessLogicMetaData("myplugin")]
[BusinessLogicMetaData("myplugin1")]
public void Test(object sender, EventArgs e)
{
    //....
}

Get exports is not returning the plugin method because of AlowMultiple=true in my MetadataAttribute. It works fine if I set my metadataAttribute to AllowMultiple = false and remove the second attribute of the plugin method.

Why I can't have two attributes on my plugin method?

Thanks for the help!

Dilshod
  • 3,189
  • 3
  • 36
  • 67

2 Answers2

2

Not sure if it will work for your particular case since I don't know your entire design and ultimate goal, but since you're creating a meta attrib and so on, you could wrap a flag enum (see Enumeration Types as Bit Flags) in your BusinessLogicMetaDataAttribute, that is, instead of using strings, use a flags enum, then you could do like below

[BusinessLogicMetaData(MyFlagEnum.myplugin | MyFlagEnum.myplugin1)]
public void Test(object sender, EventArgs e)
{
    //....
}

Update: To do multiple exports without using flag enums, inherit from Attribute instead of ExportAttribute, there are a few discussions about this problem around the web

[MetadataAttribute]
[AttributeUsage(AttributeTargets.Method, AllowMultiple = true)]
public class BusinessLogicMetaDataAttribute : Attribute, IBusinessLogicMetaData
{
     //......
}
Jason
  • 3,844
  • 1
  • 21
  • 40
  • I can't use enums. It can change by property name. So what is wrong with having two attributes? – Dilshod Jul 24 '13 at 12:03
  • @Dilshod see the update, try inheriting from `Attribute` instead – Jason Jul 24 '13 at 15:25
  • @Dilshod Note that you will have to use an ExportAttribute as well as the pure (no export) metadata attribute. – Panos Rontogiannis Jul 25 '13 at 12:26
  • @PanosRontogiannis, could you please elaborate a bit on this previous comment of yours? I am going crazy because I try to solve the exact same problem and can't find a solution. I get empty import collections when setting `AllowMultiple = true`. I tried both, inheriting from Attribute and ExportAttribute, neither works. – Matt Aug 09 '13 at 14:01
  • @MattWolf If your metadata class inherits from MetadataAttributeAttribute (no export), then you will need to decorate your exported classes with the ExportAttribute as well. On the other hand if it inherits from ExportAttribute and is decorated with the MetadataAttributeAttribute, you will, only need to decorate your exported classes using your custom metatada attribute. Check out the "Using a Custom Export Attribute" from http://mef.codeplex.com/wikipage?title=Exports%20and%20Metadata – Panos Rontogiannis Aug 10 '13 at 15:40
  • @PanosRontogiannis, please, could you take a look at the answer to my own question and let me know if that is also how you would do it? You seem to be the MEF expert and I may have just figured out how I can get it to work (through manually mapping the incoming metadata IDictionary to interface properties/custom class ) http://stackoverflow.com/questions/18132821/mef-can-i-export-import-classes-with-multiple-metadataattribute-decorations – Matt Aug 10 '13 at 16:31
0

I am answering to my own question. I decided to have two constructors on my MetaDataAttribute.

1) takes one string parameter

2) takes one string[] parameter

Then I can do like this:

[BusinessLogicMetaData(new string[]{"myplugin1", "myplugin2"})]
public void Test(object sender, EventArgs e)
{
    //....
}
Dilshod
  • 3,189
  • 3
  • 36
  • 67