具有对象

时间:2017-01-08 12:24:47

标签: c# class object

这是一个非常C#的基本问题,但我不知道如何谷歌,因为我不知道如何调用这些类。 我想定义一个类(称为ContactType),它有两个对象:ContactType.Internal& ContactType.External。并且每个对象在应用程序期间只能实例化一次,因此每次我将Contact的Type属性定义为ContactType.External或ContactType.Internal时,它都是对同一对象的引用。

此外,此ContactType类需要覆盖方法ToString(),因此当对象ContactType.Internal需要解析为字符串时,它返回“内部类型的联系人”,类似于外部对象。

3 个答案:

答案 0 :(得分:1)

如果我理解正确,这个基本原则应该能满足你的需要。需要注意的重要事项是实例是静态的,可以在任何地方访问,并且私有构造函数会阻止任何其他代码(禁止反射,你永远不会阻止它)创建自己的ContactType实例。

sealed class ContactType
{
    public static ContactType Internal { get; } = new ContactType();
    public static ContactType External { get; } = new ContactType();

    private ContactType()
    {
    }
}

如果您希望在初始化实例时涉及更多代码,则可能需要使用静态构造函数。这看起来像这样:

sealed class ContactType
{
    public static ContactType Internal { get; }
    public static ContactType External { get; }

    private ContactType()
    {
    }

    static ContactType()
    {
        // More code can fit here if creating ContactType is complicated
        Internal = new ContactType();
        External = new ContactType(); 
    }
}

静态构造函数将运行一次(在代码中首次使用类之前)并设置静态属性。

这是一个也处理ToString()功能的实现:

sealed class ContactType
{
    private readonly string contactTypeName;

    public static ContactType Internal { get; } = new ContactType("Internal");
    public static ContactType External { get; } = new ContactType("External");

    private ContactType(string contactTypeName)
    {
        this.contactTypeName = contactTypeName;
    }

    public override string ToString()
    {
        // Note that these two ways of building this string are equivalent.
        // You may be more familiar with the old commented out way, but C# 6 added this new interpolation syntax.
        // Take your pick between the two, I prefer the interpolation.

        //return "Contact of type " + this.ContactTypeName;
        return $"Contact of type {this.contactTypeName}";
    }
}

答案 1 :(得分:1)

您的问题不明确,但我认为您要找的是enum

根据您的说明,我了解您的课程为ContactContactType.ExternalContactType.Internal仅定义一次。该属性为TypeToString()应该返回Description中存储的enum值的静态Type

  

课程定义

using System.ComponentModel;

public class Contact
{
    public enum ContactType
    {
        [Description("Contact of type External")]
        External = 1,
        [Description("Contact of type Internal")]
        Internal = 2
    }
    public ContactType Type { get; set; }

    public override string ToString()
    {
        return ((DescriptionAttribute)typeof(ContactType).GetMember(Type.ToString())[0].GetCustomAttributes(typeof(DescriptionAttribute), false)[0]).Description;
    }
}
  

<强>用法

    Contact myContact = new Contact();

    myContact.Type = Contact.ContactType.External;
    txtOutput.Text = myContact.ToString();
    //Output: "Contact of type External"

    myContact.Type = Contact.ContactType.Internal;
    txtOutput.Text = myContact.ToString();
    //Output: "Contact of type Internal"

答案 2 :(得分:0)

执行此操作的旧方法是使用static variableSingleton design pattern。你仍然可以这样做。但是,在编写单元测试时,那些静态成员非常难以隔离出来。

出于这个原因,处理此问题的更现代的方法是使用Dependency Injection之类的InstancePerLifetimeScope。这样,无论代码尝试创建它们多少次,您的对象factory将始终吐出内部和外部对象的相同实例。