简体   繁体   English

将代码添加到 C# 获取/设置属性而不需要支持字段?

[英]Add code to C# get/set of property without needing backing field?

You know how you can have a property that automatically generates a backing field?您知道如何拥有自动生成支持字段的属性吗? Like if I go:就像我去:

public String SomeProperty {get; set;}

I know that if I want to add code to that property I have to create the backing field as so:我知道如果我想向该属性添加代码,我必须像这样创建支持字段:

 public string someProperty = string.Empty;
 public string SomeProperty
 {
     get { return someProperty; }
     set
     {
         someProperty = value;
         DoSomething();
     }
 }

Basically, what I want to know is... is there any way to do this but without having to create the backing field?基本上,我想知道的是......有没有办法做到这一点,但不必创建支持字段? For example I could use it to trigger some kind of event that occurs when a property is set.例如,我可以使用它来触发某种在设置属性时发生的事件。 I'm looking for something like this:我正在寻找这样的东西:

 public string SomeProperty
 {
     get;
     set { this.OnSomeEvent; }
 }

But I know that'll cause a compile error because get needs do declare a body if set does.但我知道,因为这会导致编译错误get需要做,如果声明主体, set一样。

I've researched and I cant find anything, but I thought I'd check to see if anyone knew.我已经研究过,但找不到任何东西,但我想我会检查是否有人知道。

I guess what I'm really after is some way to trigger an event when a property is changed but without having to add all that extra clutter.我想我真正想要的是在更改属性时触发事件的某种方式,但不必添加所有额外的混乱。 Any suggestions?有什么建议么?

Simple answer is no, you can't have it both ways.简单的答案是否定的,你不能同时拥有它。 From .NET Docs :来自.NET 文档

In C# 3.0 and later, auto-implemented properties make property-declaration more concise when no additional logic is required in the property accessors.在 C# 3.0 及更高版本中,当属性访问器中不需要额外的逻辑时,自动实现的属性使属性声明更加简洁

There are not any solutions for this built into the framework, and you cannot modify existing types via reflection (in order to add the logic at runtime).框架中没有为此内置任何解决方案,并且您不能通过反射修改现有类型(以便在运行时添加逻辑)。 The only way to accomplish this seems to be at compile time.实现这一点的唯一方法似乎是在编译时。

There is a product http://www.postsharp.net/ that can accomplish this (intercept property/method calls), and there does appear to be a free edition.有一个产品http://www.postsharp.net/可以做到这一点(拦截属性/方法调用),而且似乎有一个免费版本。

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

相关问题 C#属性,是否可以在没有定义set(没有后备变量)的情况下定义get? - C# property, is it possible to get around defining get without defining set (no backing variable)? 在给定支持字段的情况下获取C#auto属性的PropertyInfo - Get PropertyInfo of a C# auto property given the backing field 自动实现的get中的C#支持字段背后的代码是什么? 组;? - What's the code behind a C# backing field in a auto implemented get; set;? 在C#中的设置支持字段中乘以值 - Multiply Value in Set Backing Field in C# 正确编码直接访问Property C的支持字段# - Proper Coding Direct Access to the backing field of a Property C# C# - 自动属性和返回支持字段之间的区别? - C# - Difference between Automatic Property and returning a backing field? C#中公共属性支持私有字段的XML序列化 - XML Serialization of public property backing private field in C# 在 C# 中,我如何确保在不将计算属性封装在另一个类中的情况下,我永远不会直接将其分配给计算属性的支持字段? - How do I, in C#, ensure I never assign to a computed property's backing field directly without encapsulating it in another class? c#using字段使用get和set方法vs使用property - c# using field with get and set methods vs using property 代码生成器工具,用于生成属性和支持字段 - Code generator tool to generate a property and backing field
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM