简体   繁体   English

如何将 Class 公共属性设置为类型列表<t> ?</t>

[英]How can I set a Class Public Property as Type List<T>?

I want to define a public property in a User Control with a type of List that I can pass a List to and then have the control bind it to a repeater.我想在用户控件中定义一个公共属性,其类型为 List,我可以将 List 传递给该属性,然后让控件将其绑定到转发器。

public partial class RepeaterPager : System.Web.UI.UserControl
{
    public List<T> DataSource;
}

The from calling code来自调用代码

List<someClass> list = new List<someClass>;
RepeaterPager1.DataSource = list ;

I thought this would be simple, but I am getting Error The type or namespace name 'T' could not be found (are you missing a using directive or an assembly reference?) on the line that declares the public property.我认为这很简单,但在声明公共属性的行上出现错误找不到类型或命名空间名称“T”(您是否缺少 using 指令或程序集引用?)。 What am I doing wrong我究竟做错了什么

Cheers干杯

Stewart斯图尔特

This is only possible if the class containing property is generic.仅当包含属性的 class 是通用的时,这才有可能。 In theory, you could use generic method:理论上,您可以使用通用方法:

void SetDataSource<T>(List<T> dataSource)

but you'll lose type information elsewhere.但是您会在其他地方丢失类型信息。

Maybe,也许,

IEnumerable DataSource

will be a better option?会是更好的选择吗?

You can't have generic parameters in fields of a class with out the class being generic.如果 class 是通用的,则不能在 class 的字段中使用通用参数。

See this answer for info on making a usercontrol generic: C# generics usercontrol有关使用户控件通用的信息,请参阅此答案: C# generics 用户控件

Or you could just use a non-generic version, such as IEnumerable or IList .或者您可以只使用非通用版本,例如IEnumerableIList

You either need to define generic Type, or make RepeaterPager generic您要么需要定义泛型类型,要么使 RepeaterPager 泛型

public partial class RepeaterPager<T> : System.Web.UI.UserControl
{
    public List<T> DataSource;
}

The general pattern for this type of data binding is to define DataSource as type object.此类数据绑定的一般模式是将 DataSource 定义为类型 object。 Then, in the set method you can do a runtime check to verify the object is of a type you expect -- throw an exception or debug assert otherwise.然后,在 set 方法中,您可以进行运行时检查以验证 object 是否属于您期望的类型——否则抛出异常或调试断言。

Besides, Repeater.DataSource is type object anyways...此外, Repeater.DataSource无论如何都是类型 object ......

Many times I can get away without holding a reference to the datasource, so I pass it directly to the control:很多时候我可以在不持有对数据源的引用的情况下逃脱,所以我将它直接传递给控件:

public object DataSource
{
    get { return myRepeater.DataSource; }
    set {
        if (value is IEnumerable) // or whatever your requirement is, if needed
            myRepeater.DataSource = value;
        else
            throw new NotSupportedException("DataSource must be an IEnumerable type");
    }
}

However, if you really do need to know about the type T, I would consider the following:但是,如果您确实需要了解类型 T,我会考虑以下内容:

  1. If possible, use the generic class idea others have suggested.如果可能,请使用其他人建议的通用 class 想法。
  2. If you need to repeat this pattern for different classes, build a base class or Interface for all your data sources to derive from如果您需要为不同的类重复此模式,请为您的所有数据源构建一个基础 class 或接口以从中派生
  3. Use DataSource as type object, but just grab the type at run time using obj.GetType() .使用 DataSource 作为类型 object,但只需在运行时使用obj.GetType()获取类型。 (Depending on your needs this may or may not work) (根据您的需要,这可能会或可能不会起作用)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 可访问性不一致:属性类型-如果类和列表都是公共的,则如何解决 - Inconsistent accessibility: property type - How to solve it if both class and list was public 为什么我不能在静态类中访问公共属性字段 - Why can't I access public property field in static class 如何在派生类中隐藏基类公共属性 - How can I hide a base class public property in the derived class 如何判断类属性是否具有公共集(.NET)? - How do I tell if a class property has a public set (.NET)? 无法访问班级公共财产 - Can't access class public property 无法访问课程的公共财产 - Can't access public property of a class 如何隐藏列表 class 中的属性 - How can I hide property in list class 当T是具有未知参数的泛型类时,如何访问类型T的对象上的属性? - How can I access property on object of type T, when T is a generic class with unknown parameter? 如何为列表创建索引访问器 <T> 我在自定义课程中拥有的物业? - How can I create an index accessor for a List<T> Property I have in a custom class? 如何在LINQ中将Entity类型的自定义属性设置为Entities查询,并且仍然返回IQueryable <T> ? - How can I set custom property of an Entity type in LINQ to Entities query and still return a IQueryable<T>?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM