时间:2010-07-24 09:47:17

标签: c# .net dependency-injection unity-container

2 个答案:

答案 0 :(得分:3)

答案 1 :(得分:0)

根据之前的回答,我提出了创建TypeConverter来访问任何静态属性的想法。这是我的实施:

TypeConverter实施

using System;
using System.ComponentModel;
using System.Reflection;

namespace EntLibUnity.Extensions
{
    /// <summary>
    /// Converts an (formatted) string to a reference of a given static member.
    /// The string uses this format: {member}@{assemblyQualifiedName}
    /// For example:
    ///     Version@System.Environment, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089
    /// </summary>
    public class StringToStaticInstanceTypeConverter : TypeConverter
    {
        public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
        {
            return (typeof(string) == sourceType) || base.CanConvertFrom(context, sourceType);
        }

        public override object ConvertFrom(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value)
        {
            string stringValue;
            if (value == null
                || (stringValue = value as string) == null
                || string.IsNullOrEmpty(stringValue)
                || !stringValue.Contains("@")
            )
            {
                return null;
            }

            var stringParts = stringValue.Split('@');
            if (stringParts.Length != 2)
            {
                return null;
            }
            var staticProperty = stringParts[0];
            var assemblyQualifiedName = stringParts[1];
            var staticClassType = Type.GetType(assemblyQualifiedName, true);
            var staticPropertyInfo = staticClassType.GetProperty(staticProperty,
                                                                 BindingFlags.Public | BindingFlags.Static |
                                                                 BindingFlags.FlattenHierarchy);
            var staticValue = staticPropertyInfo.GetValue(null, null) ?? base.ConvertFrom(context, culture, value);
            return staticValue;
        }
    }
}

用法示例: 让我们假设以下接口和具体实现:

namespace EntLibUnity.UnitySample
{
    public class VersionManager : IVersionManager
    {
        private readonly Version _version;

        public string Version
        {
            get { return _version.ToString(); }
        }

        public VersionManager(Version version)
        {
            _version = version;
        }
    }
}

namespace EntLibUnity.Infrastructure
{
    public interface IVersionManager
    {
        string Version { get; }
    }
}

我们的具体版本依赖于System.Version,因此我们将容器配置为提供此值。在我的示例中,我将使用配置文件来实现此目的。

<?xml version="1.0" encoding="utf-8" ?>
<unity xmlns="http://schemas.microsoft.com/practices/2010/unity">
  <!-- Using Design-Time Configuration: http://msdn.microsoft.com/en-us/library/ff660935(v=PandP.20).aspx#format_config -->
  <!-- The Unity Configuration Schema: http://msdn.microsoft.com/en-us/library/ff660914(v=PandP.20).aspx -->
  <!-- Specifying Types in the Configuration File http://msdn.microsoft.com/en-us/library/ff660933(v=PandP.20).aspx#_Default_Aliases_and-->

  <alias alias="StringToStaticInstanceTypeConverter" type="EntLibUnity.Extensions.StringToStaticInstanceTypeConverter, EntLibUnity.Extensions" />
  <alias alias="IVersionManager" type="EntLibUnity.Infrastructure.IVersionManager, EntLibUnity.Infrastructure" />
  <alias alias="VersionManager" type="EntLibUnity.UnitySample.VersionManager, EntLibUnity.UnitySample" />

  <container>
    <register type="IVersionManager" mapTo="VersionManager">
      <constructor>
        <param name="version">
          <value value="Version@System.Environment, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" typeConverter="StringToStaticInstanceTypeConverter" />
        </param>
      </constructor>
    </register>
  </container>

</unity>

最后,一旦加载了配置,就可以使用如下:

var versionManager = container.Resolve<IVersionManager>();
Console.WriteLine(versionManager.Version);
祝你好运