绑定 IList不显示 IMyInterface 继承的接口成员

发布于 2024-07-13 16:58:11 字数 1202 浏览 7 评论 0原文

我将 IList 绑定到 GridView。 IMyInterface 看起来像

public interface IMyInterface: IHasTotalHours, IHasLines
{
    DateTime GoalStartDate { get; set; }
    DateTime GoalEndDate { get; set; }
}

我将一个实例绑定到网格,如下所示:

IList<IMyInterface> instance= GetMyData();

myGrid.DataSource = instance;
myGrid.DataBind();

将其绑定到网格时,网格中显示的唯一成员是 IMyInterface 的直接成员:GoalStartDate 和 GoalEndDate。

这是为什么? 如何让网格显示它继承的其他接口的成员?

更新 继承的接口定义简单的数据属性,例如

public interface IHasTotalHours
{
    string Description { get; set; }
    int Hours{ get; set; }
}
public interface IHasLines
{
    double TotalLines { get; set; }
    double LinesPerHour { get; set; }
}

There is a class that Implements IMyInterface:

public class MyClass : IMyInterface
{
    public string Description { get; set; }
    public int Hours { get; set; }
    public double TotalLines { get; set; }
    public double LinesPerHour { get; set; }
    public DateTime GoalStartDate { get; set; }
    public DateTime GoalEndDate { get; set; }

}

这些被强制转换为 IMyInterface,并在我绑定到 GridView 的列表中返回。

I'm binding IList to a GridView. IMyInterface looks like

public interface IMyInterface: IHasTotalHours, IHasLines
{
    DateTime GoalStartDate { get; set; }
    DateTime GoalEndDate { get; set; }
}

I bind an instance to a Grid like this:

IList<IMyInterface> instance= GetMyData();

myGrid.DataSource = instance;
myGrid.DataBind();

When bind this to the grid, the only members that show up in the grid are the direct members of IMyInterface: GoalStartDate and GoalEndDate.

Why is that? How do I get the grid to display the members of the other interfaces it inherits?

Update
The inherited interfaces define simple data properties like

public interface IHasTotalHours
{
    string Description { get; set; }
    int Hours{ get; set; }
}
public interface IHasLines
{
    double TotalLines { get; set; }
    double LinesPerHour { get; set; }
}

There is a class that implements IMyInterface:

public class MyClass : IMyInterface
{
    public string Description { get; set; }
    public int Hours { get; set; }
    public double TotalLines { get; set; }
    public double LinesPerHour { get; set; }
    public DateTime GoalStartDate { get; set; }
    public DateTime GoalEndDate { get; set; }

}

These are cast as IMyInterface, and returned in the list that I'm binding to the GridView.

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

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

发布评论

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

评论(2

淡淡的优雅 2024-07-20 16:58:11

数据绑定控件不使用反射,而是使用 TypeDescriptor 从数据源获取属性。 在 TypeDescriptor.GetProperties 方法中,您可以读取以下内容:

组件的属性可以
与类的属性不同,
因为网站可以添加或删除
属性(如果组件已定位)。

显然,默认实现只会从接口返回直接属性,而不是继承的属性。

幸运的是,这种机制是可扩展的,您可以编写一个 TypeConverter 具有自定义属性信息实现的类。 属性逻辑的​​实现请参考TypeConverter文档中的备注。

自定义 TypeConverter 类的 GetProperties 实现可以在您的接口及其所有继承接口上调用 TypeDescriptor.GetProperties(Type)。 但也许您甚至可以编写一个通用的 TypeConverter,它可以使用反射查找所有继承的属性。

然后,您可以使用 TypeConverterAttribute 属性将此自定义 TypeConverter 附加到您的界面。

然后,就像魔术一样,数据源将找到所有属性。 ;-)

Data bound controls do not use reflection but a TypeDescriptor to get the properties from a data source. In the TypeDescriptor.GetProperties method, you can read the following:

The properties for a component can
differ from the properties of a class,
because the site can add or remove
properties if the component is sited.

Apparently the default implementation will only return direct properties from an Interface and not the inherited ones.

Luckily this mechanism is extensible, and you can write a TypeConverter class with custom property information implementation. Please refer to the remarks in the TypeConverter documentation for implementing property logic.

The GetProperties implementation of your custom TypeConverter class can call TypeDescriptor.GetProperties(Type) on your interface and all it's inherited interfaces. But maybe you could even write a generic TypeConverter that would find all inherited properties by using reflection.

Then you attach this custom TypeConverter to your interface with the TypeConverterAttribute attribute.

And then, like magic, the data source will find all properties. ;-)

不寐倦长更 2024-07-20 16:58:11

这是因为接口是一个契约,与对象交互的唯一方法是通过该特定契约。 在进行强制转换之前,无法假定并使用其他接口。

因此,当您将 T 列表绑定到某些内容时,数据网格不知道那些其他接口。 并且数据网格不会使用反射来确定可能继承哪些其他类或接口。 数据网格可用的唯一对象属性是 T 接口公开的属性。

如果您希望数据网格能够访问所有属性,则需要绑定 List。

It's because an interface is a contract, and that's the only way to interact with an object is through that specific contract. The other interfaces cannot be assumed and can't be utilized until a cast is made.

So when you bind a List of T to something, the datagrid doesn't know about those other interfaces. And the datagrid isn't going to use reflection to figure out what other classes or interfaces might be inherited. The only object properties that are going to be available to the datagrid are the properties exposed by the T interface.

You need to bind List if you want the datagrid to have access to all the properties.

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