是否可以将属性添加到部分类中的属性?

Is it possible to add an attribute to a property in a partial class?(是否可以将属性添加到部分类中的属性?)
本文介绍了是否可以将属性添加到部分类中的属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不认为这是可能的,但由于我没有从 MSDN 得到明确的说明,我觉得最好问问.假设我们有一个如下的类.

I don't think it's possible but since I haven't got a definite clarity from MSDN, I feel that it's best to ask. Suppose that we have a class as follows.

public partial class Hazaa
{
  public int Shazoo { get; set; }
}

然后,我希望将 Shazoo 归为 SuperCool 我希望在另一个文件中这样做.由于我使用的是部分类,因此我可以按如下方式添加新属性.

Then, I'd like Shazoo to be attributed as SuperCool but I wish to do so in another file. Since I'm using partial classes, I can add new properties as follows.

public partial class Hazaa
{
  [SuperCool]
  public int Wheee { get; set; }
}

但是我可以通过在第一个示例中编写代码来为第一个示例中声明的属性赋予属性吗?我怀疑这是可能的,但我很乐意接受纠正.如果是这样,语法是什么?

But can I attribute a property declared in the first sample by writing code in the latter? I doubt it's possible but I'll be glad to stand corrected. If so, what's the syntax?

推荐答案

不,你不能.

您只能将属性附加到您在那里声明的成员,然后,除非该成员也被声明为部分(以便您可以在其他地方重新实现它),否则您不能将属性附加到另一个部分文件中声明的成员.

You can only attach attributes to members you declare there and then, and unless the member is also declared as partial (so that you may reimplement it elsewhere) you cannot attach attributes to members declared in another partial file.

这篇关于是否可以将属性添加到部分类中的属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

DispatcherQueue null when trying to update Ui property in ViewModel(尝试更新ViewModel中的Ui属性时DispatcherQueue为空)
Drawing over all windows on multiple monitors(在多个监视器上绘制所有窗口)
Programmatically show the desktop(以编程方式显示桌面)
c# Generic Setlt;Tgt; implementation to access objects by type(按类型访问对象的C#泛型集实现)
InvalidOperationException When using Context Injection in ASP.Net Core(在ASP.NET核心中使用上下文注入时发生InvalidOperationException)
LINQ many-to-many relationship, how to write a correct WHERE clause?(LINQ多对多关系,如何写一个正确的WHERE子句?)