需要帮助理解泛型、如何抽象类型问题

发布于 2024-08-24 04:47:11 字数 2037 浏览 3 评论 0原文

我可以使用一些非常好的链接来解释泛型以及如何使用它们。但我还有一个非常具体的问题,与当前项目的工作有关。

给定此类构造函数:

    public class SecuredDomainViewModel<TDomainContext, TEntity> : DomainViewModel<TDomainContext, TEntity>
        where TDomainContext : DomainContext, new()
        where TEntity : Entity, new()

    public SecuredDomainViewModel(TDomainContext domainContext, ProtectedItem protectedItem)
            : base(domainContext)
        {
            this.protectedItem = protectedItem;
        }

及其创建方式如下:

                DomainViewModel d;
                d = new SecuredDomainViewModel<MyContext, MyEntityType>(this.context, selectedProtectedItem);

假设我在 MyContext 中有 20 个不同的 EntityType,是否有更简单的方法可以在没有大型 switch 语句的情况下调用构造函数?

另外,由于 d 是 DomainViewModel 并且我稍后需要从 SecuredDomainViewModel 访问方法,看来我需要这样做:

if (((SecuredDomainViewModel<MyContext, MyEntityType>)d).CanEditEntity)

但“MyEntityType”实际上可能是 20 种不同类型之一。无论如何,是否可以编写从某种反射返回 MyEntityType 的这些类型的语句?

需要澄清的附加信息: 我将调查 ConstructorInfo,但我认为我可能错误地描述了我想要做的事情。

假设我有 DomainViewModel, d 在我原来的帖子中。

这可能是通过三种可能的方式构建的:

d = new SecuredDomainViewModel<MyContext, Order>(this.context, selectedProtectedItem);

d = new SecuredDomainViewModel<MyContext, Invoice>(this.context, selectedProtectedItem);

d = new SecuredDomainViewModel<MyContext, Consumer>(this.context, selectedProtectedItem);

稍后,我需要访问 SecuredDomainViewModel 上的方法,当前必须以这种方式调用:

ex: if (((SecuredDomainViewModel<MyContext, Order)d).CanEditEntity)
ex: if (((SecuredDomainViewModel<MyContext, Invoice)d).CanEditEntity)
ex: if (((SecuredDomainViewModel<MyContext, Consumer)d).CanEditEntity)

假设我在此上下文中有 N+ 实体类型,我希望能够做的是 一次调用类似于这样:

ex: if (((SecuredDomainViewModel<MyContext, CurrentEntityType)d).CanEditEntity)

其中 CurrentEntityType 是某种函数或其他类型的调用,它根据当前项目实体类型返回订单、发票或消费者。

这可能吗?

I could use some really good links that explain Generics and how to use them. But I also have a very specific question, relater to working on a current project.

Given this class constructor:

    public class SecuredDomainViewModel<TDomainContext, TEntity> : DomainViewModel<TDomainContext, TEntity>
        where TDomainContext : DomainContext, new()
        where TEntity : Entity, new()

    public SecuredDomainViewModel(TDomainContext domainContext, ProtectedItem protectedItem)
            : base(domainContext)
        {
            this.protectedItem = protectedItem;
        }

And its creation this way:

                DomainViewModel d;
                d = new SecuredDomainViewModel<MyContext, MyEntityType>(this.context, selectedProtectedItem);

Assuming I have 20 different EntityTypes within MyContext, is there any easier way to call the constructor without a large switch statement?

Also, since d is DomainViewModel and I later need to access methods from SecuredDomainViewModel, it seems I need to do this:

if (((SecuredDomainViewModel<MyContext, MyEntityType>)d).CanEditEntity)

But again "MyEntityType" could actually be one of 20 diffent types. Is there anyway to write these types of statements where MyEntityType is returned from some sort of Reflection?

Additional Info for Clarification:
I will investigate ConstructorInfo, but I think I may have incorrectly described what I'm looking to do.

Assume I have the DomainViewModel, d in my original posting.

This may have been constructed via three possible ways:

d = new SecuredDomainViewModel<MyContext, Order>(this.context, selectedProtectedItem);

d = new SecuredDomainViewModel<MyContext, Invoice>(this.context, selectedProtectedItem);

d = new SecuredDomainViewModel<MyContext, Consumer>(this.context, selectedProtectedItem);

Later, I need to access methods on the SecuredDomainViewModel, which currently must be called this way:

ex: if (((SecuredDomainViewModel<MyContext, Order)d).CanEditEntity)
ex: if (((SecuredDomainViewModel<MyContext, Invoice)d).CanEditEntity)
ex: if (((SecuredDomainViewModel<MyContext, Consumer)d).CanEditEntity)

Assuming I have N+ entity types in this context, what I was hoping to be able to do is
something like this with one call:

ex: if (((SecuredDomainViewModel<MyContext, CurrentEntityType)d).CanEditEntity)

Where CurrentEntityType was some sort of function or other type of call that returned Order, Invoice or Consumer based on the current item entity type.

Is that possible?

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

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

发布评论

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

评论(2

甜心小果奶 2024-08-31 04:47:11

您可以创建一个具有 CanEditEntity 属性的非通用接口,使 SecuredDomainViewModel 继承该接口,然后通过该接口调用该属性...

此外,新的() 构造函数允许您调用没有参数的泛型类型的构造函数(因此您只需编写 new TEntity()),但如果您想调用具有参数的构造函数,有一个方便的技巧我使用的是将其作为委托传递:

public void Method<T>(Func<string, bool, T> ctor) {
    // ...
    T newobj = ctor("foo", true);
    // ...
}

//called later...
Method((s, b) => new MyClass(s, b));

You can create a non-generic interface that has the CanEditEntity property on it, make SecuredDomainViewModel inherit off that, then call the property through the interface...

Also, the new() constructor allows you to call a constructor on a generic type that has no arguments (so you can just write new TEntity()), but if you want to call a constructor that has parameters one handy trick I use is to pass it in as a delegate:

public void Method<T>(Func<string, bool, T> ctor) {
    // ...
    T newobj = ctor("foo", true);
    // ...
}

//called later...
Method((s, b) => new MyClass(s, b));
亢潮 2024-08-31 04:47:11

我无法提供链接,也可能无法提供类型。

构造函数

如果你有 Type,你可以得到构造函数:

ConstructorInfo construtor = typeof(MyEntityType).GetConstructor(new object[]{TDomainContext, ProtectedItem});

Type

我不太确定你在寻找什么,但我只能看到类似

if (((SecuredDomainViewModel<MyContext, entityType>)d).CanEditEntity)
{
    entityType=typeof(Orders)
}

你想要的东西。

I can't help on the links, and likely not on the type either.

Constructor

If you have the Type, you can get the constructor:

ConstructorInfo construtor = typeof(MyEntityType).GetConstructor(new object[]{TDomainContext, ProtectedItem});

Type

I'm not really sure what you're looking for, but I can only see something like

if (((SecuredDomainViewModel<MyContext, entityType>)d).CanEditEntity)
{
    entityType=typeof(Orders)
}

being what you want.

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