简体   繁体   English

在C#中直接引用时,如何让类使用默认的getter / setter?

[英]How to have a class use a default getter/setter when referenced directly in C#?

Let's say I have a class like 假设我有一个像

class SecretInt
{
    private int secret = 1;
}

How can I make it so that. 我如何做到这一点。

SecretInt a = new SecretInt();
SecretInt b = new SecretInt();
Console.Write(a + b);

Would output 2? 会输出2吗?

Is there something like 是否有类似的东西

Class SecretInt()
{
    int secret = 1;
    get { return secret; }
    set { secret = value; }
}

So that direct references to the class would use the getters and setters rather than the class itself? 因此,直接引用该类将使用getter和setter而不是类本身吗?

You can do this in two step. 您可以分两步执行此操作。

  1. Overload the + operator for your class 重载课程的+ operator

     public static SecretInt operator +(SecretInt s1, SecretInt s2) { var s = new SecretInt(); s.secret = s1.secret + s2.secret; return s; } 
  2. Then override ToString method: 然后重写ToString方法:

     public override string ToString() { return this.secret.ToString(); } 

Take a look at Operator Overloading Tutorial for more details. 有关更多详细信息,请参阅《 操作员重载教程 》。

Overload the + operator so sums extract the real value 重载+运算符,以便求和得出真实值

http://msdn.microsoft.com/en-us/library/aa288467(v=vs.71).aspx http://msdn.microsoft.com/zh-CN/library/aa288467(v=vs.71).aspx

Unfortunately C# does not implement the concept of Default Properties that we see in VB.NET or similar. 不幸的是,C#没有实现我们在VB.NET或类似版本中看到的默认属性的概念。 The solutions generally given for this are to use conversion operators to create new instances of the class... but you lose the rest of the properties and state. 通常为此提供的解决方案是使用转换运算符创建类的新实例...,但是您会丢失其余的属性和状态。

For instance, this VB.NET class: 例如,此VB.NET类:

public class MyClass
    Public myNumber As Integer
    Private myString As String
    Default Property myProperty As String
        Get
            return myString
        End Get
        Set(ByVal Value As String)
            myString = Value
        End Set
    End Property
End Class

In VB you can then do: 在VB中,您可以执行以下操作:

Dim i As New MyClass
i.myInteger = 10
i = "Hello"

The VB.NET compiler locates the default property and uses that as the target of the assignment. VB.NET编译器查找默认属性,并将其用作分配的目标。

Now consider the same code in C#, operating on the same class: 现在考虑在C#中使用相同的代码,对相同的类进行操作:

var i = new MyClass();
i.myInteger = 10;
i = "Hello";

Since C# doesn't know about the default property, this fails to compile. 由于C#不了解默认属性,因此无法编译。 Using the standard conversion technique we can define a conversion operator that looks like this: 使用标准转换技术,我们可以定义如下所示的转换运算符:

public static implicit operator MyClass(string value)
{
    return new MyClass { myProperty = value };
}

Now we can do i = "Hello"; 现在我们可以i = "Hello"; and the myProperty value will of course be set... but the myInteger field will now be reinitialised to default. 并且当然会设置myProperty值...,但是myInteger字段现在将重新初始化为默认值。

The sad thing is that We can decorate a C# class with DefaultPropertyAttribute to tell VB.NET which property it should use as the default when working with our class, but we can't use that information in C# without some reflection techniques. 可悲的是,我们可以使用DefaultPropertyAttribute装饰一个C#类,以告诉VB.NET使用该类时应使用哪个属性作为默认属性,但是如果没有一些反射技术,我们就无法在C#中使用该信息。

The other answers here give workarounds to solve the specific case you listed, but there is no solution to give the same Default Property functionality that the VB.NET language has. 此处的其他答案提供解决方案,以解决您列出的特定情况,但是没有提供与VB.NET语言相同的默认属性功能的解决方案。 Unless you want to start hacking on Roslyn and create your own version of C# :P 除非您想在Roslyn上开始黑客攻击并创建自己的C#版本:P

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM