在运行时更改 C# 中字段的类型

发布于 2024-08-10 08:39:52 字数 764 浏览 9 评论 0原文

我有一个现有的类层次结构,这样说:

Business
  - Division
    - ProjectTeam
      - Employee

这些类是通过反序列化实例化的。

但是,现在我需要为库的特定用户公开 Employee 中的额外字段,即说这样的话:

SpecialBusiness (extends Business)
  - Division
    - ProjectTeam
      - SpecialEmployee (extends Employee)
        - Degree

问题是,我不能只创建一个扩展“Business”的类,因为我想要添加的是到“员工”类。

在我看来,我有两个选择:

  1. 用“特殊”类复制层次结构。这意味着每个“特殊”类将具有原始类的集合和新“特殊”类的集合。

    特殊业务
      - 分区和特殊分区(扩展分区)
        - 项目团队和特殊项目团队(扩展项目团队)
          - 员工和特殊员工(扩展员工)
            - 程度
    
  2. 出于反序列化的目的,在运行时将“Employee”重新键入“SpecialEmployee”。请注意,我可以将代码库中所有“Employee”对象从 SpecialBusiness 转换为“SpecialEmployee”(可能使用辅助方法使其变得明显)。

关于如何处理这个问题有什么想法吗?

I have an existing heirarchy of classes, say something like this:

Business
  - Division
    - ProjectTeam
      - Employee

These classes are instantiated via deserialization.

However, now I need to expose extra fields in the Employee for a particular user of the library i.e. say something like this:

SpecialBusiness (extends Business)
  - Division
    - ProjectTeam
      - SpecialEmployee (extends Employee)
        - Degree

The problem is, I can't just make a class that extends 'Business' because the addition I want to make is to the 'Employee' class.

As I see it I have two options:

  1. Duplicate the heirarchy with 'Special' classes. This means each 'Special' class will have a collection of the original classes and a collection of the new 'Special' classes.

    SpecialBusiness
      - Division AND SpecialDivision (extends Division)
        - ProjectTeam AND SpecialProjectTeam (extends ProjectTeam)
          - Employee AND SpecialEmployee (extends Employee)
            - Degree
    
  2. Somehow retype 'Employee' to 'SpecialEmployee' at runtime for deserialization purposes. Know that I can cast all 'Employee' objects from a SpecialBusiness to 'SpecialEmployee' in the codebase (possibly using helper methods to make it obvious).

Any ideas on how to deal with this problem?

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

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

发布评论

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

评论(7

七婞 2024-08-17 08:39:52

您无法在运行时更改字段的类型。

也就是说,如果你想做的话,为什么不能直接延长员工人数呢? 多态性表示您可以插入 a 的对象子类型转换为它的超类型的字段?为什么需要重写父对象?

You cannot change the type of a field at runtime.

That said, why can't you just extend employee if that's what you want to do? Polymorphism states you can plug an object of a subtype into a field of it's supertype? Why do you need to rewrite the parent object?

夜声 2024-08-17 08:39:52

好吧,我不完全确定它是否适合您的情况,但请看看我对 这个问题。您可以从 Decorable 派生您的 Division 和 Employee 类,并从 DecoratorDecorator 派生 SpecialDivision 和 SpecialEmployee 类。

Well, I'm not completely sure if it is suitable in your situation, but have a look at my answer for this question. You can derive your Division and Employee classes from Decorable and derive SpecialDivision and SpecialEmployee from Decorator<Division> and Decorator<Employee>.

浪推晚风 2024-08-17 08:39:52

我不知道你现在是否可以这样做,但是整个对象结构应该使用接口而不是具体的类。

I don't know if it's possible for you to do now, but the whole object structure should use interfaces instead of concrete classes.

韶华倾负 2024-08-17 08:39:52

大多数序列化框架(例如 XmlSerializer 和 BinaryFormatter)都提供了以自定义方式反序列化流的方法,因此您可以拥有更新/新的层次结构并将旧流反序列化到其中。您使用哪种序列化框架?

Most serialization frameworks, e.g. XmlSerializer and BinaryFormatter, provide ways to deserialize streams in a custom way, so you can have your updated/new hierarchy and deserialize old streams into it. What kind of serialization framework are you using?

待天淡蓝洁白时 2024-08-17 08:39:52

为什么不能修改反序列化源来实例化运行时所需的类?

Why can't you muck with your source for deserialization to instantiate the classes that you want at runtime?

爱已欠费 2024-08-17 08:39:52

当 XML 将实际对象类型声明为:

  <Employees>
    <Employee>
      <Name>Bob</Name>
    </Employee>
    <Employee xsi:type="SpecialEmployee">
      <Name>Carol</Name>
      <Degree>Chief</Degree>
    </Employee>
  </Employees>

至少在延迟声明类型(此处为 Employee[]Employees)时,XMLSerializer 很好地支持反序列化时的多态性,因为它将创建一个 Name="Carol" 且 Degree="Chief" 的 SpecialEmployee ;给定的 SpecialEmployee 是从 Employee 派生的,并且 XMLSerializer 被告知这一点。
这可以通过 XmlInclude 属性来完成,也可以通过在创建 XMLSerializer 时提供专用类型来完成。

When the XML states the actual objects types as:

  <Employees>
    <Employee>
      <Name>Bob</Name>
    </Employee>
    <Employee xsi:type="SpecialEmployee">
      <Name>Carol</Name>
      <Degree>Chief</Degree>
    </Employee>
  </Employees>

at least when deferring from declared type (here Employee[] Employees) the XMLSerializer well supports polymorphism at deserialization, as it will create a SpecialEmployee with Name="Carol" and Degree="Chief"; given SpecialEmployee is derived from Employee and the XMLSerializer is told that.
This can either be done by XmlInclude attribute or by providing the specialised types when creating the XMLSerializer.

眼眸 2024-08-17 08:39:52

一种可能的解决方案(假设选择的反序列化方法允许私有字段序列化):

使用问题中的方法 1(即单独的层次结构)并将类的扩展版本存储在私有字段中。反序列化后,使用扩展版本更新常规公共非扩展字段,并包含返回这些字段的强制转换版本的方法。

SpecialProjectTeam
  - (private) SpecialEmployees
  - (public) Employees
  - (public) GetSpecialEmployees (returns Employees field cast as SpecialEmployees)

One possible solution (assumes chosen deserialisation method allows private field serialization):

Use the method 1 from the question (i.e. a separate hierarchy) and store the extended versions of the classes in a private field. After deserialization update the regular public non-extended fields with the extended versions and include a method that returns a cast version of these fields.

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