autofac

/*
  User: Peter
  Date: 2015/8/10
  Time: 19:54
 
                           
*/   
using System;
using System.Linq;
using System.Collections;
using System.Collections.Generic;
using Autofac.Extras.Attributed;
using Autofac.Features.Metadata;
using System.ComponentModel.Composition;
using System.Reflection;
using System.ComponentModel;
using Autofac;


namespace testAutofac
{
    
    class Testdict:Dictionary<string,object>
    {
        public Testdict(string name)
        {
            this["name"] = name;
        }
    }
    [MetadataAttribute]
    class LetterFormatterAttribute : Attribute
    {
        public string Name { get; private set; }
        public int Age { get; private set; }
     
        public LetterFormatterAttribute(IDictionary<string, object> dict)
        {
            Name = dict["Name"].ToString();
            
            Age =int.Parse(dict["Age"].ToString());
            
        }
     
        public LetterFormatterAttribute(string name,int Age)
        {
            Name = name;
            this.Age = Age;
        }
     
    }

    interface ILetterFormatter
    {
        string FormatLetter(string content);
    }
    
    
    [LetterFormatter("Personal2",20)]
    class ImpersonalLetterFormatter : ILetterFormatter
    {
        public ImpersonalLetterFormatter()
        {
            Console.WriteLine("ImpersonalLetterFormatter");
        }
        public string FormatLetter(string content)
        {
            return "To Whom It May Concern:nn" + content;
        }
    }

    [LetterFormatter("Personal",10)]
    class PersonalLetterFormatter : ILetterFormatter
    {
        public PersonalLetterFormatter()
        {
            Console.WriteLine("PersonalLetterFormatter");
        }
        public string FormatLetter(string content)
        {
            return "Dear Individual,nn" + content;
        }
        
        
    }
    
    interface ITop
    {
         string   FormatLetter(string name ,string content);
    }

     class Top:ITop
    {
        IEnumerable<Meta<Lazy<ILetterFormatter>, LetterFormatterAttribute>> _letters;
        public Top(IEnumerable<Meta<Lazy<ILetterFormatter>, LetterFormatterAttribute>> 
                   letters)
        {
            _letters = letters;
        }
        public string   FormatLetter(string name ,string content)
        {
            var letterPersonal = _letters.First(
                x=>
                {
                    Console.WriteLine(x.Metadata.Age);
                    return x.Metadata.Name==name ;
                    
                }
            );
            return letterPersonal.Value.Value.FormatLetter(content);
        }
    }


    class Program
    {
        public static void Main(string[] args)
        {
             var builder = new ContainerBuilder();
 
            builder.RegisterModule<AttributedMetadataModule>();
 
            builder.RegisterAssemblyTypes(typeof(Program).Assembly)
        
                .AsImplementedInterfaces();
            
            
            var container = builder.Build();
            var top
                = container.Resolve<ITop>();
            
            Console.WriteLine(top.FormatLetter("Personal2","aaa"));
            Console.ReadKey(true);
        }
    }
}

主要测试autofac 的注入

IEnumerable<Meta<Lazy<ILetterFormatter>, LetterFormatterAttribute>>

一个接口实现多类,用Attribute来判断那个可以使用。

  <package id="Autofac" version="3.1.0" targetFramework="net40" />
  <package id="Autofac.Extras.Attributed" version="3.1.0" targetFramework="net40" />

Attribute类一定要有一个构造有参数IDictionary<string, object>, autofac通过这个反射入值。

原文地址:https://www.cnblogs.com/peteryu007/p/4727847.html