Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/cocoa/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C#子类丢失属性值_C#_Generics_Exception_Abstract Class - Fatal编程技术网

C#子类丢失属性值

C#子类丢失属性值,c#,generics,exception,abstract-class,C#,Generics,Exception,Abstract Class,由于几天前的一个问题(你可以查看这篇文章),我需要创建一种方法,使我的一些linq到sql表能够被动态引用。我已经通过一个抽象的LogTable类实现了这一点,它包含了我的DataTable上定义的所有LogTable属性。我之所以能够用这种抽象的方式,是因为我的日志表具有相同的结构,因此具有相同的属性 以下是简化代码: 抽象基类 更新日志表的My(简化)动态方法: public void UpdateLog<T>(T currentLOG) where T : LogTableSt

由于几天前的一个问题(你可以查看这篇文章),我需要创建一种方法,使我的一些linq到sql表能够被动态引用。我已经通过一个抽象的LogTable类实现了这一点,它包含了我的DataTable上定义的所有LogTable属性。我之所以能够用这种抽象的方式,是因为我的日志表具有相同的结构,因此具有相同的属性

以下是简化代码:

抽象基类

更新日志表的My(简化)动态方法:

public void UpdateLog<T>(T currentLOG) where T : LogTableStructure
    {

        LogTableStructure logStructure = null;

        //LogTableEnum is defined on this class constructor
        switch (LogTableEnum)
        {
            case LogTableEnum.Log2009:
                logStructure = this.factory.LogDB.LOG_2009s
                    .SingleOrDefault(q => q.ID == currentLOG.ID);
                break;
            case LogTableEnum.Log2010:
                logStructure = this.factory.LogDB.LOG_2010s
                    .SingleOrDefault(q => q.ID == currentLOG.ID);
                break;
            case LogTableEnum.Log2011:
                logStructure = this.factory.LogDB.LOG_2011s
                    .SingleOrDefault(q => q.ID == currentLOG.ID);
                break;
   }
}
public void UpdateLog(T currentLOG),其中T:LogTableStructure
{
LogTableStructure logStructure=null;
//LogTableEnum在此类构造函数上定义
开关(LogTableEnum)
{
案例LogTableEnum.Log2009:
logStructure=this.factory.LogDB.LOG_2009s
.SingleOrDefault(q=>q.ID==currentLOG.ID);
打破
案例LogTableEnum.Log2010:
logStructure=this.factory.LogDB.LOG\u 2010s
.SingleOrDefault(q=>q.ID==currentLOG.ID);
打破
案例LogTableEnum.Log2011:
logStructure=this.factory.LogDB.LOG_2011s
.SingleOrDefault(q=>q.ID==currentLOG.ID);
打破
}
}
问题

出于某种原因,currentLOG参数抛出运行时null引用异常,即使它已填充了所有日志表属性。通过使用vs2010 debbuger,我注意到当currentLOG属性被填充时,基类(LogTableStructure)属性都是空的,就好像基类对象是空的一样


我是不是忘记了关于成员隐藏继承或类似的东西?我甚至在.dbml上的所有日志表属性中添加了
新的
修饰符,但即使这样也没有解决问题

您的排序似乎有误。将null赋值给logStructure,然后从中调用InjectFrom。只有这样,您才能将工厂创建的实例分配给该变量。

您的排序似乎有误。将null赋值给logStructure,然后从中调用InjectFrom。只有这样,才能将工厂创建的实例分配给该变量。

只需将实际实例注入到对象中:

// This object is going to have to be a class that inherits from your abstract class
// It can't be a null object of the type of your abstract class.
var logStructure = new InstanceOfLogTableStructure();
logStructure.InjectFrom(currentLOG);

这样就可以了。

只需创建要向其中注入实际实例的对象:

// This object is going to have to be a class that inherits from your abstract class
// It can't be a null object of the type of your abstract class.
var logStructure = new InstanceOfLogTableStructure();
logStructure.InjectFrom(currentLOG);


应该可以了。

你能举一个调用
UpdateLog
方法的代码示例吗?另外,您在哪里定义要注入的
ocorreciabanco
对象?什么是
ocorreciabanco
?为什么要声明一个
logStructure
变量,然后忽略它?“currentLOG参数抛出[…]”是什么意思?(参数本身不能引发异常-只有access可以。)您知道上一个case语句中的输入错误吗?我猜应该是Log2011。
LogTableStructure logStructure=null;logStructure.InjectFrom(currentLOG)UpdateLog
方法的代码示例吗?另外,您在哪里定义要注入的
ocorreciabanco
对象?什么是
ocorreciabanco
?为什么要声明一个
logStructure
变量,然后忽略它?“currentLOG参数抛出[…]”是什么意思?(参数本身不能引发异常-只有access可以。)您知道上一个case语句中的输入错误吗?我猜应该是Log2011。
LogTableStructure logStructure=null;logStructure.InjectFrom(currentLOG)