父级从子级获取信息并生成子级的共享方法?

发布于 2024-09-09 07:53:30 字数 882 浏览 2 评论 0原文

这就是我想要实现的目标。然而,在折腾了一天半之后,我几乎确信这是不可能完成的。如果有人能告诉我如何实现这一目标,我将非常感激。

我有一个具有共享方法 Fetch() 的 .NET 父类 Parent。 Fetch 可以采用各种参数来帮助它知道要获取哪些特定记录,但这目前无关紧要。

Fetch() 需要知道要查找哪个表来查找其后面的记录。对于继承 Parent 的每个子类,该表都不同。我想将表名保留在作为 Child 类的属性的字符串中。然后每个子级都可以提供自己的表名,而父级可以处理获取,这对所有子级都以相同的方式工作。

执行查询后,Fetch() 应该有一个 DataReader。所有 Child 类都有一个构造函数,该构造函数可以接收 DataReader 作为参数,并使用它来生成其自身的实例,其中属性由读取器填充。

我必须强调的一件事: Fetch 是一种共享方法。如果我从头开始设计它,我可能不会这样做,但事实上我正在重构一些绝对的白痴(他们可能是也可能不是两年前的我)的代码去构建了一些项目,这些项目使用 Fetch() 作为每个子类(或者更确切地说,我想要成为这个新父类的子类的每个类)的共享方法,并且不必这样做肯定会很好将这些项目中的每个事件更改为实例。

编辑: 我具体遇到的是,我无法弄清楚如何将表名放入 Fetch() 中,也不知道如何确定子类以便在生成的 DataReader 上调用其构造函数。

回顾一下:

  • 父级具有共享的 Fetch() 方法
  • 方法的调用方式与 Child.Fetch() 中一样
  • 父级从子级获取表信息
  • Fetch() 知道子级的类名,因此它可以使用它获取的 DataReader 调用构造函数。

能做到吗?非常感谢。

Here's what I'd like to achieve. After mucking around with it for a day and a half, though, I've become nearly convinced that it can't be done. If someone can show me how to achieve this, I'd be quite grateful.

I have a .NET parent class Parent with a shared method Fetch(). Fetch can take a variety of parameters to help it know what particular records to fetch, but that's irrelevant at the moment.

Fetch() needs to know what table to look in to find the records its after. The table is different for each child class that inherits Parent. I'd like to keep the table name in a string that's a property of the Child class. Then each Child can provide its own table name, and Parent can handle the Fetching which works the same way for all of them.

Once it's executed the query, Fetch() should then have a DataReader. All Child classes have a constructor which can receive a DataReader as a parameter and use it to produce an instance of itself with attributes populated from the reader.

One thing I have to stress:
Fetch is a Shared method. If I were designing it from scratch, I might not make it so, but as it is I'm refactoring the code of some absolute moron (who may or may not have been me 2 years ago) who went and built a handful of projects that use Fetch() as a shared method of each Child class (or rather, each class that I want to make into a child of this new Parent), and it sure would be nice not to have to change every occurrence in those projects to instances.

EDIT:
What I'm running into specifically is that I cannot figure out how to get the tablename into Fetch(), nor how to determine the child class in order to call its constructor on the resulting DataReader.

So to recap:

  • Parent has Shared Fetch() method
  • Method is called as in Child.Fetch()
  • Parent gets table info from Child
  • Fetch() knows class name of Child so it can call the constructor with the DataReader it fetches.

Can it be done? Many thanks.

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

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

发布评论

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

评论(2

So尛奶瓶 2024-09-16 07:53:30

父级可以具有子级需要实现的 MustOverride 属性,该属性可以包含表信息。这解决了父级如何知道要查找每个子级的表的问题,但您不能拥有 MustOverride Shared 成员,因此您需要子级的实例才能在父级中使用。

也许您可以在父级上使用通用的 Fetch,根据类型参数创建正确类型的 Child 实例。考虑到正确的限制,这当然是可能的。

Parent can have a MustOverride property that Child would be required to implement, that could contain the table information. That solves the problem of how Parent knows what table to look in for each Child, but you cannot have a MustOverride Shared member, so you'd need instances of Child to work with in Parent.

Maybe you could have a generic Fetch on parent that created instances of Child of the correct type based on the type parameter. It's certainly possible given the correct constraints.

败给现实 2024-09-16 07:53:30

如果 Fetch 知道子类的全名,那么它应该能够使用 反思来掌握孩子的类型,从那里我希望你会发现它一帆风顺。

If Fetch knows the full name of the child class then it should be able to use reflection to get hold of the type of the child and from there I would expect you'll find it plain sailing.

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