zoukankan      html  css  js  c++  java
  • Unity的类型实例创建

    Unity提供了四个配置节来完成类型示例的创建。使用示例类型如下:

    public interface ILogger
    {
      void Write(String text);
      void WriteLine(String text);
    }
    
    public sealed class MyLogger : ILogger
    {
      public MyLogger(String path)
      {
    
      }
    
      public Int32 FileSize { get; set; }
    
      public void Initialize(Int32 capacity)
      {
    
      }
    
      #region ILogger Members
    
      public void Write(String text)
      {
        throw new NotImplementedException();
      }
    
      public void WriteLine(String text)
      {
        throw new NotImplementedException();
      }
    
      #endregion
    }

    1、lifetime,管理类型实例的生命周期。Unity默认提供了几种实现,比如常用的singleton(单例)、perthread(ThreadStatic)等。

    <register type=”UnityTest2.ILogger, UnityTest2″ mapTo=”UnityTest2.MyLogger, UnityTest2″>
      <lifetime type=”perthread” />
    </register>

    开发人员可以通过继承于LifetimeManager自定义生命周期管理。

    2、constructor,指定构造函数。比如MyLogger提供了一个参数的构造函数,参数名称为path:

    <register type=”UnityTest2.ILogger, UnityTest2″ mapTo=”UnityTest2.MyLogger, UnityTest2″>
      <lifetime type=”perthread” />
      <constructor>
        <param name=”path” value=”D:\” />
      </constructor>
    </register>

    3、property,当类型实例构造函数执行完后,Unity会根据property的配置初始化对应的值。

    <register type=”UnityTest2.ILogger, UnityTest2″ mapTo=”UnityTest2.MyLogger, UnityTest2″>
      <lifetime type=”perthread” />
      <constructor>
        <param name=”path” value=”1″ />
      </constructor>
      <property name=”FileSize” value=”10″ />
    </register>

    4、method,当类型实例构造函数、属性赋值完成后,Unity会根据method调用不同的函数进行初始化。

    <register type=”UnityTest2.ILogger, UnityTest2″ mapTo=”UnityTest2.MyLogger, UnityTest2″>
      <lifetime type=”perthread” />
      <constructor>
        <param name=”path” value=”1″ />
      </constructor>
      <property name=”FileSize” value=”10″ />
      <method name=”Initialize”>
        <param name=”capacity” value=”10″ />
      </method>
    </register>

    constructor的param、property、method都允许定义多个。

  • 相关阅读:
    用JavaDoc生成项目文档
    thymeleaf参考手册
    转的一个Java基本功
    杂记
    修改Esxi克隆的CentOS的IP地址
    CentOS搭建socket5代理服务器
    CentOS上搭建Nginx + Mono 运行 asp.net
    启动PPT的时候一直配置vs2013的问题解决
    swift 元组
    swift 集合类型(二)
  • 原文地址:https://www.cnblogs.com/junchu25/p/2631500.html
Copyright © 2011-2022 走看看