可以在C#中使用通用形式吗? [英] Can you use generic forms in C#?

查看:35
本文介绍了可以在C#中使用通用形式吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您应该能够创建通用表单:

You should be able to create a generic form:

public partial class MyGenericForm<T> :
    Form where T : class
{
    /* form code */
    public List<T> TypedList { get; set; }
}

是有效的C#,并进行编译.但是,如果您有任何图像表明无法找到资源,则设计器将无法工作,并且表单将抛出运行时异常.

Is valid C#, and compiles. However the designer won't work and the form will throw a runtime exception if you have any images stating that it cannot find the resource.

我认为这是因为Windows窗体设计器假定资源将以简单类型的名称存储.

I think this is because the windows forms designer assumes that the resources will be stored under the simple type's name.

推荐答案

可以!这是我前段时间通过技巧制作的博客文章:

Yes you can! Here's a blog post I made a while ago with the trick:

设计通用表单

看起来您已经用这种方式了.这种方法很好用,所以我不会觉得它太笨拙.

Looks like you're already doing it this way. This method works fine so I wouldn't consider it too hacky.

这篇关于可以在C#中使用通用形式吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆