C# 如何覆盖部分类属性

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15207652/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 14:30:12  来源:igfitidea点击:

How to override a partial class property

c#asp.net-mvc-4entity-framework-5

提问by Mike Valstar

I have a partial class and I want to do something like the following:

我有一个部分课程,我想做如下事情:

[MetadataType(typeof(UserMetaData))]
public partial class Person
{
    public override string PrivateData
    {
        get
        {
            return customDecrypt(base.PrivateData);
        }
        set
        {
            base.PrivateData = customEncrypt(value);
        }
    }
}

the above does not work however.

但是,上述方法不起作用。

Is there a way to override the base entity framework classes its properties to allow for custom getter/setter?

有没有办法覆盖基本实体框架类的属性以允许自定义 getter/setter?

采纳答案by Bobson

Partial classes have nothing to do with inheritance, and overrideis entirely about inheritance.

部分类与继承无关,override完全是关于继承的。

The partialkeyword on a class just means that it can be declared multiple times in the same assembly. It's exactly the same as if you copied every part of every partial classinto the same file and removed the partialkeyword. Since you can't define the same property/function/etc twice in the same class, you can't define it twice in two separate parts of the same class, even with the partialkeyword.

partial类上的关键字只是意味着它可以在同一个程序集中多次声明。这与将每个部分的每个部分都复制partial class到同一个文件中并删除partial关键字完全相同。由于您不能在同一个类中两次定义相同的属性/函数/等,因此您不能在同一个类的两个不同部分中定义两次,即使使用partial关键字也是如此。

override, on the other hand, is used in derivedclasses to indicate that they're replacing the functionality of the base class they inherit from. If it doesn't explicitly inherit, it inherits from object, which lets you override ToString()(among others).

override另一方面,在派生类中使用,表示它们正在替换它们继承的基类的功能。如果它没有显式继承,则它继承自object,这使您可以覆盖ToString()(以及其他)。

Your best options to do what you want are either to use a custom T4 template to generate the encrypt/decrypt logic, or to set the encrypted properties to protectedor privatein the designer and manually add publicversions which do the decryption.

执行所需操作的最佳选择是使用自定义 T4 模板生成加密/解密逻辑,protected或者private在设计器中或在设计器中设置加密属性并手动添加public执行解密的版本。

回答by Roman Pokrovskij

If you can change code generator, you can easy "emulate" it ("overriding" default values) using extrimely powerfull partial methods.

如果您可以更改代码生成器,则可以使用极其强大的部分方法轻松“模拟”它(“覆盖”默认值)。

https://msdn.microsoft.com/en-us/library/wa80x488.aspx

https://msdn.microsoft.com/en-us/library/wa80x488.aspx

回答by Jon Roberts

While you can't override the Entity Framework base classes, there is a work around. In the .edmx model (for DB first, direct in entities if code first) on the propery you want to 'override' change the Getter / Setter to private and rename the property. Then create the partial class with the property using the public name, which will no longer conflict.

虽然您无法覆盖实体框架基类,但有一个变通方法。在 .edmx 模型中(首先对于 DB,如果代码首先直接在实体中)中您想要“覆盖”的属性将 Getter/Setter 更改为私有并重命名属性。然后使用公共名称创建具有属性的分部类,这将不再发生冲突。

In the public partial class property you will be able to access the private renamed property if needed.

在公共部分类属性中,如果需要,您将能够访问私有重命名的属性。