I have a Linq to SQL DataContext with SQL functions into it. While the retrieve object is good, I would like to add a property to the object and same time shortens the object name. So I made an object Reminder that inherits from the Linq to SQL object fnRTHU_ReminderByTaskResult with the property I wanted.
我有一个带有SQL函数的Linq to SQL DataContext。虽然检索对象很好,但我想向对象添加属性,同时缩短对象名称。所以我创建了一个对象Reminder,它继承了Linq to SQL对象fnRTHU_ReminderByTaskResult和我想要的属性。
public class Reminder : fnRTHU_ReminderByTaskResult
{
public string MyProperty {get;set;}
}
Then when the function is bind to the list I try in the aspx file to do:
然后当函数绑定到列表时,我尝试在aspx文件中执行:
<asp:Literal runat="server" Text='<%# ((Reminder)DataBinder.Container).MyProperty %>' />
Where DataBinder.Container is of type fnRTHU_ReminderByTaskResult. This results in an InvalidCastException, unable to cast object type fnRTHU_ReminderByTaskResult to type Reminder. I don't understand since I inherit from the type I'm casting from.
DataBinder.Container的类型为fnRTHU_ReminderByTaskResult。这会导致InvalidCastException,无法将对象类型fnRTHU_ReminderByTaskResult强制转换为类型提醒。我不明白,因为我继承了我正在铸造的类型。
3 个解决方案
#1
Since the Linq to Sql function returns that base class, your object was never that of your derived class. Therefore, when trying to cast down, it can't do it. You can however provide implicit and explicit casting implementations within your class that would allow this cast to work.
由于Linq to Sql函数返回该基类,因此您的对象永远不是派生类的对象。因此,在试图抛弃时,它无法做到。但是,您可以在类中提供允许此强制转换工作的隐式和显式强制转换实现。
#2
The problem is your attempting to case an instance of a Base class to a Child class. This is not valid because Child inherits from Base. There is no guarantee that a case from Base to Child will work (this is known as a downcaste). Up casts (Child to Base) are safe.
问题是您试图将一个Base类的实例添加到Child类。这是无效的,因为Child继承自Base。无法保证从基地到儿童的案件能够奏效(这被称为降级)。向上演员(儿童到基地)是安全的。
This will work though if the Container property is an instance of Reminder. In this case, it does not appear to be.
如果Container属性是Reminder的实例,这将有效。在这种情况下,它似乎不是。
#3
Unfortunately you are attempting to downcast which C# does not support. You are not able to cast from a parent type to a child type.
不幸的是,你试图贬低哪个C#不支持。您无法从父类型转换为子类型。
#1
Since the Linq to Sql function returns that base class, your object was never that of your derived class. Therefore, when trying to cast down, it can't do it. You can however provide implicit and explicit casting implementations within your class that would allow this cast to work.
由于Linq to Sql函数返回该基类,因此您的对象永远不是派生类的对象。因此,在试图抛弃时,它无法做到。但是,您可以在类中提供允许此强制转换工作的隐式和显式强制转换实现。
#2
The problem is your attempting to case an instance of a Base class to a Child class. This is not valid because Child inherits from Base. There is no guarantee that a case from Base to Child will work (this is known as a downcaste). Up casts (Child to Base) are safe.
问题是您试图将一个Base类的实例添加到Child类。这是无效的,因为Child继承自Base。无法保证从基地到儿童的案件能够奏效(这被称为降级)。向上演员(儿童到基地)是安全的。
This will work though if the Container property is an instance of Reminder. In this case, it does not appear to be.
如果Container属性是Reminder的实例,这将有效。在这种情况下,它似乎不是。
#3
Unfortunately you are attempting to downcast which C# does not support. You are not able to cast from a parent type to a child type.
不幸的是,你试图贬低哪个C#不支持。您无法从父类型转换为子类型。