zoukankan      html  css  js  c++  java
  • 自定义容器,支持创建普通实例,单例,线程单例

    自定义容器:

    public interface ITestContainer
    {
    void RegisterType<IT, T>();
    void RegisterType<IT, T>(LifeTimeType lifeTime);
    IT Resolve<IT>();
    }

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Reflection;
    using System.Runtime.Remoting.Messaging;
    using System.Text;
    using System.Threading.Tasks;

    namespace Container
    {
    public class TestContainer:ITestContainer
    {
    private Dictionary<string, RegisterInfo> typeContainer = new Dictionary<string, RegisterInfo>();
    private Dictionary<string, object> instanceDic = new Dictionary<string, object>();
    public void RegisterType<IT, T>()
    {
    this.RegisterType<IT, T>(LifeTimeType.Common);
    }

    public void RegisterType<IT, T>(LifeTimeType lifeTime)
    {
    if (!typeContainer.ContainsKey(typeof(IT).FullName))
    typeContainer.Add(typeof(IT).FullName, new RegisterInfo()
    {
    Type = typeof(T),
    LifeTimeType = lifeTime
    });
    }
    public IT Resolve<IT>()
    {
    string key = typeof(IT).FullName;
    if (!typeContainer.ContainsKey(typeof(IT).FullName))
    {
    throw new Exception("没有为{key}的初始化");
    }

    RegisterInfo info = typeContainer[key];
    IT t = default(IT);
    switch (info.LifeTimeType)
    {
    case LifeTimeType.Common:
    {
    Type type = info.Type;
    t = (IT)Resolve(type);
    break;
    }
    case LifeTimeType.Sigleton:
    {
    if (!instanceDic.ContainsKey(key))
    {
    Type type = info.Type;
    t = (IT)Resolve(type);
    instanceDic.Add(key, t);
    }
    else
    t = (IT)instanceDic[key];
    break;
    }

    case LifeTimeType.PerThread:
    {
    //获取当前线程
    object obj = CallContext.GetData(key);
    if (obj == null)
    {
    Type type = info.Type;
    t = (IT)Resolve(type);
    CallContext.SetData(key, t);//设置当前线程
    }
    else
    t = (IT)obj;
    break;
    }
    default:
    break;
    }
    return t;

    }

    public object Resolve(Type type)
    {
    var ctorArray = type.GetConstructors();
    ConstructorInfo ctor = null;
    if (ctorArray.Count(c => c.IsDefined(typeof(InjectionConstractorAttribute), true)) > 0)
    {
    //获取有标记属性InjectionConstractorAttribute的构造函数
    ctor = ctorArray.FirstOrDefault(c => c.IsDefined(typeof(InjectionConstractorAttribute), true));
    }
    else
    {
    //获取构造器参数最多的构造函数
    ctor = ctorArray.OrderByDescending(c => c.GetParameters().Length).FirstOrDefault();
    }

    var paraList = ctor.GetParameters();
    Object[] paramArray = new object[paraList.Length];
    int i = 0;
    foreach (var param in paraList)
    {
    Type interfaceType = param.ParameterType;
    if (typeContainer.ContainsKey(interfaceType.FullName))
    {
    Type paramType = this.typeContainer[interfaceType.FullName].Type;
    var ParaObject = Resolve(paramType);
    paramArray[i] = ParaObject;
    i++;
    }
    }
    return Activator.CreateInstance(type, paramArray);

    }
    }

    public enum LifeTimeType
    {
    Common = 0,
    Sigleton = 1,
    PerThread = 2
    }

    public class RegisterInfo
    {
    public Type Type { get; set; }
    public LifeTimeType LifeTimeType { get; set; }
    }
    }

    测试自定义容器:

    //自定义容器实现:公用,单例,线程单例
    IPhone phone6 = null;
    IPhone phone7 = null;
    IPhone phone8 = null;
    IPhone phone9 = null;
    IPhone phone10 = null;
    ITestContainer testContainer = new TestContainer();
    testContainer.RegisterType<IPhone, AndroidPhone>(LifeTimeType.PerThread);
    phone6 = testContainer.Resolve<IPhone>();
    phone7 = testContainer.Resolve<IPhone>();
    Console.WriteLine($"67的当前线程ID:{Thread.CurrentThread.ManagedThreadId}");

    new Action(() =>
    {
    phone8 = testContainer.Resolve<IPhone>();
    Console.WriteLine($"8的当前线程ID:{Thread.CurrentThread.ManagedThreadId}");
    }).BeginInvoke(null, null);

    new Action(() =>
    {
    phone9 = testContainer.Resolve<IPhone>();
    Console.WriteLine($"9的当前线程ID:{Thread.CurrentThread.ManagedThreadId}");
    }).BeginInvoke(arg =>
    {
    phone10 = testContainer.Resolve<IPhone>();
    Console.WriteLine($"10的当前线程ID:{Thread.CurrentThread.ManagedThreadId}");
    }, null);
    Thread.Sleep(5000);
    //如果注册的时候参数:空,结果全部为False
    //如果注册的时候参数:LifeTimeType.Sigleton,结果全部为 true
    //如果注册的时候参数:LifeTimeType.PerThread,结果全部为 true,false,false,false,true
    Console.WriteLine($"67:{object.ReferenceEquals(phone6, phone7)}");
    Console.WriteLine($"68:{object.ReferenceEquals(phone6, phone8)}");
    Console.WriteLine($"69:{object.ReferenceEquals(phone6, phone9)}");
    Console.WriteLine($"89:{object.ReferenceEquals(phone8, phone9)}");
    Console.WriteLine($"910:{object.ReferenceEquals(phone9, phone10)}");
    Console.ReadLine();

  • 相关阅读:
    css笔记
    CSS基础
    HTML常用基础标签
    React-Native中列表SectionList学习
    React-Native中列表FlatList配合网络请求fetch学习
    React-Native选项卡(react-navigation)之createBottomTabNavigator学习
    React-Native导航条(react-navigation)之createStackNavigator学习
    iOS中touch事件传递(响应者链)
    ios中UIView和CALayer关系
    iOS中KVO使用理解
  • 原文地址:https://www.cnblogs.com/fblogs/p/12263443.html
Copyright © 2011-2022 走看看