我正在阅读一些代码,请您解释以下内容的作用?
bool isFeatureEnabled = FeatureControl.Current.Features.AppDesigner.IsEnabled(organizationId,currentOrgDBVersion);
这里是上述代码的定义
public sealed class FeatureControl : IFeatureControl
{
public static IFeatureControl Current { get; }
[XmlIgnore]
public IFeatureDetailContainer Features { get; set; }
....
}
public interface IFeatureControl
{
IFeatureDetailContainer Features { get; set; }
...
}
public interface IFeatureDetailContainer
{
IFeatureDetail AppDesigner { get; }
}
public interface IFeatureDetail
{
bool IsEnabled(Guid organizationId, Version currentOrgDBVersion);
}
我没有看到任何创建的实例,这是如何工作的?
抱歉,我复制了元数据,我刚刚找到了实际代码:
public sealed class FeatureControl : IFeatureControl
{
private static readonly Lazy<IFeatureControl> current = new Lazy<IFeatureControl>(() => new FeatureControl());
private IFeatureDetailContainer features;
public static IFeatureControl Current
{
get
{
return current.Value;
}
}
/// <summary>
/// Accessor to the Features List for Developers to retrieve the information
/// </summary>
[XmlIgnore]
public IFeatureDetailContainer Features
{
get
{
return this.features;
}
set
{
this.features = value;
}
}
}
答案 0 :(得分:0)
这是一种单身模式。通常,实例是在构造函数中创建的。
public interface IFeatureControl { }
public sealed class FeatureControl : IFeatureControl
{
public static IFeatureControl Current { get; }
static FeatureControl()
{
if (Current == null)
{
Current = new FeatureControl();
}
}
}
[TestFixture]
public class FeatureControlTests
{
[Test]
public void IsFeatureControlSingleton()
{
IFeatureControl c1 = FeatureControl.Current;
IFeatureControl c2 = FeatureControl.Current;
Assert.AreSame(c1, c2);
}
}
答案 1 :(得分:0)
在代码中的某个位置(此处未显示),您可以预期正在创建/新建对象 IFeatureControl :: Current 。
您的代码行只是访问该值。请注意,如果您在没有实际实例化当前对象的情况下运行代码,则会获得null ref。错误。
您可以使用Interfaces编写一组精心设计的代码,代码将编译并且看起来很棒,但是如果没有任何接口对象使用从Interface继承的new'd实例实例化,那么您将获得null引用异常。
考虑在这个例子中使用接口来概述如何安排事情以及它们将如何运作。然而,它只是一个轮廓,你需要在线条内部着色才能真正实现结果。 祝你好运!