具有非自动属性的 C# 匿名支持字段

发布于 2024-11-28 04:07:47 字数 495 浏览 0 评论 0原文

我想创建一个私有成员变量,即使对于拥有它的类也是私有的,并且只能由其 getter 和 setter 访问。 我知道你可以使用像这样的自动属性来做到这一点

private int MyInt{ get; set;}

,但我希望能够修改 getter 和 setter,这样(例如)我可以记录该字段被设置的次数(即使是由所属类)。像这样的东西

private int MyInt
{
    get{ return hiddenValue; }
    set{ hiddenValue = value; Console.Out.WriteLine("MyInt has been set");}
}

,其中“hiddenValue”是只能在 getter 和 setter 中访问的成员。 为什么?因为我是一个偏执的防御性程序员,我什至不相信自己:p。

这在 C# 中可能吗?如果是这样,语法是什么?

谢谢。

I want to make a private member variable that is private even to the class that owns it, and can ONLY be accessed by its getters and setters.
I know you can do this with auto-properties like

private int MyInt{ get; set;}

But I want to be able to modify the getter and setter so (for example) I could log how many times the field has been set (even by the owning class). Something like this

private int MyInt
{
    get{ return hiddenValue; }
    set{ hiddenValue = value; Console.Out.WriteLine("MyInt has been set");}
}

where "hiddenValue" is the member that is only accessible in the getter and setter.
Why? because I'm a paranoid defensive programmer, I don't even trust myself :p.

Is this possible in C#? and if so, what is the syntax?

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

擦肩而过的背影 2024-12-05 04:07:47

你真的应该相信自己。

不,您不能将变量设置为私有,即使封装类也看不到它。

如果您确实想要这个,您可以将该值封装在一个嵌套类中,该类将能够覆盖其自己的私有属性。

class Foo 
{
    class Bar // nested
    {
        private int _value;
        public int Value 
        {
            get { return _value; }
            set { _value = value; /* logic */ }
        }
    }
}

Foo 可以实例化一个 Bar,获取 bar.Value,但无法获取 _value。

You really should trust yourself.

And no, you can't make a variable so private even the encapsulating class can't see it.

If you really want this, you could encapsulate the value in a nested class, which would be able to cover its own privates.

class Foo 
{
    class Bar // nested
    {
        private int _value;
        public int Value 
        {
            get { return _value; }
            set { _value = value; /* logic */ }
        }
    }
}

Foo can instantiate a Bar, get at bar.Value, but it cannot get to _value.

无敌元气妹 2024-12-05 04:07:47

.Net 中的任何语言都是不可能的。而且很好。防御性编码是好的,但是当变得偏执时,它会让其他需要维护它的开发人员发疯。

Is not possible with any language in .Net. And is good. Defensive coding is good, but when becomes paranoid, it makes go crazy other developers who need to maintain it.

国产ˉ祖宗 2024-12-05 04:07:47

面向方面编程 (AOP) 可能会有所帮助,简而言之,它允许您在方法调用之前和/或之后注入您选择的代码。

查看 http://code.google.com/p/easyprop/,它是一个 AOP专门为观察属性变化而设计的库,并且有使用自动属性的示例。

注意:我还没有使用过这个库,但它看起来很适合你。

Aspect-Oriented Programming (AOP) might help, which in a nutshell, allowing you to inject code of your choice before and/or after method calls.

Check out http://code.google.com/p/easyprop/ which is an AOP library specifically designed for watching property changes, and there is examples for working with auto-properties.

NB: I haven't used the library yet, but it looks right up your alley.

蒗幽 2024-12-05 04:07:47

不,这在 C# 中是不可能的。

你最好的选择可能是变得不那么偏执。 :P

No, this isn't possible in C#.

Your best bet is probably to become less paranoid. :P

彼岸花ソ最美的依靠 2024-12-05 04:07:47

您正在尝试做的事情在.NET 中无法实现。

甚至隐藏的支持字段在 IL 代码中也是可见的。这意味着虽然你在设计时(在 VS 中)看不到它,但它会在编译后出现。

What you are trying to do, can not be achieved in .NET.

Even the hidden backing field will be visible in IL code. Meaning while you can't see it at design time (in VS) it will be there after the compile.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文