Click here to Skip to main content
15,897,891 members
Home / Discussions / C#
   

C#

 
Questionhow to add meta tags dynamically using query string in asp.net? Pin
iamvinod346-Apr-15 23:47
iamvinod346-Apr-15 23:47 
Questionopen a url using windows form Pin
Sudarshan M S6-Apr-15 22:54
Sudarshan M S6-Apr-15 22:54 
AnswerRe: open a url using windows form Pin
OriginalGriff6-Apr-15 23:17
mveOriginalGriff6-Apr-15 23:17 
AnswerRe: open a url using windows form Pin
Nitin Gupta17-Apr-15 0:20
Nitin Gupta17-Apr-15 0:20 
QuestionMessage Removed Pin
6-Apr-15 10:30
professionalN_tro_P6-Apr-15 10:30 
QuestionFactory Pattern... Why? Pin
Kevin Marois6-Apr-15 8:46
professionalKevin Marois6-Apr-15 8:46 
AnswerRe: Factory Pattern... Why? Pin
Sascha Lefèvre6-Apr-15 9:33
professionalSascha Lefèvre6-Apr-15 9:33 
GeneralRe: Factory Pattern... Why? Pin
harold aptroot6-Apr-15 9:54
harold aptroot6-Apr-15 9:54 
GeneralRe: Factory Pattern... Why? Pin
Kevin Marois6-Apr-15 9:58
professionalKevin Marois6-Apr-15 9:58 
GeneralRe: Factory Pattern... Why? Pin
harold aptroot6-Apr-15 10:09
harold aptroot6-Apr-15 10:09 
AnswerRe: Factory Pattern... Why? Pin
CHill606-Apr-15 9:57
mveCHill606-Apr-15 9:57 
AnswerRe: Factory Pattern... Why? Pin
Abhinav S6-Apr-15 16:52
Abhinav S6-Apr-15 16:52 
AnswerRe: Factory Pattern... Why? Pin
Eddy Vluggen6-Apr-15 21:41
professionalEddy Vluggen6-Apr-15 21:41 
AnswerRe: Factory Pattern... Why? Pin
Nathan Minier8-Apr-15 2:30
professionalNathan Minier8-Apr-15 2:30 
GeneralRe: Factory Pattern... Why? Pin
BillWoodruff9-Apr-15 9:27
professionalBillWoodruff9-Apr-15 9:27 
GeneralRe: Factory Pattern... Why? Pin
Nathan Minier13-Apr-15 1:54
professionalNathan Minier13-Apr-15 1:54 
Sure,

First we need an interface to hold the metadata that we want to have our queries based on. In this case, we'll just use a name variable:
C#
public interface IExportMetadata
{
   string Name { get; }
}

Then generally I'll tag an export that I plan to provide multiple instances for with a custom ExportAttribute, such as:

[MetadataAttribute, AttributeUsage(AttributeTargets.Class, AllowMultiple = false)]
C#
public class MyAttribute : ExportAttribute
{
   public string Name { get; set; }

   public MyAttribute(Type type, string name) : base(type) { Name = name; }
}

Then I can tag anything I export with a name so that it can be targeted:
C#
[My(typeof(IMyInterface),"Test!")]
public class MyClass
{
   ...
}

And then my factory can provide it to a host application based on a specific request or a configuration:
C#
public static class MefProvider
{
   private static readonly Lazy<CompositionContainer> _container =
            new Lazy<CompositionContainer>(() => new CompositionContainer(_catalog.Value));

   private static readonly Lazy<AggregateCatalog> _catalog =
            new Lazy<AggregateCatalog>(() => new AggregateCatalog(
            new AssemblyCatalog(Assembly.GetExecutingAssembly())));

   public static CompositionContainer Container
   {
       get { return _container.Value; }
   }

   public static T Provide<T>(string name = "")
   {
       if (string.IsNullOrWhiteSpace(name))
       {
           return _container.Value.GetExportedValueOrDefault<T>();
       }
       return Container.GetExports<T, IExportMetadata>()
           .Where(x => x.Metadata.Name
           .Equals(name, StringComparison.InvariantCultureIgnoreCase))
           .Select(y => y.Value)
           .FirstOrDefault();
    }
}

QuestionReading and storing application settings in an object at startup Pin
Member 115846886-Apr-15 6:44
Member 115846886-Apr-15 6:44 
AnswerRe: Reading and storing application settings in an object at startup Pin
Gerry Schmitz6-Apr-15 12:27
mveGerry Schmitz6-Apr-15 12:27 
GeneralRe: Reading and storing application settings in an object at startup Pin
Member 115846887-Apr-15 4:57
Member 115846887-Apr-15 4:57 
GeneralRe: Reading and storing application settings in an object at startup Pin
Gerry Schmitz7-Apr-15 9:06
mveGerry Schmitz7-Apr-15 9:06 
GeneralRe: Reading and storing application settings in an object at startup Pin
Member 115846887-Apr-15 8:54
Member 115846887-Apr-15 8:54 
GeneralRe: Reading and storing application settings in an object at startup Pin
Gerry Schmitz7-Apr-15 10:30
mveGerry Schmitz7-Apr-15 10:30 
GeneralRe: Reading and storing application settings in an object at startup Pin
Member 115846888-Apr-15 2:43
Member 115846888-Apr-15 2:43 
GeneralRe: Reading and storing application settings in an object at startup Pin
Gerry Schmitz8-Apr-15 10:49
mveGerry Schmitz8-Apr-15 10:49 
AnswerRe: Reading and storing application settings in an object at startup Pin
Nathan Minier8-Apr-15 2:37
professionalNathan Minier8-Apr-15 2:37 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.