如何在摘要class c#中设置值

how to set value in abstract class c#

我正在尝试为 AbstractArAdjustmentLine 列表赋值 属性 但出现错误

Object reference not set

下面是通过dll生成的2个classes,class结构我改不了

public class ArAdjustmentCreate : AbstractArAdjustment
{
    public ArAdjustmentCreate(string controlId = null);
    public override void WriteXml(ref IaXmlWriter xml);
}

public abstract class AbstractArAdjustment: AbstractFunction
{
    public List<AbstractArAdjustmentLine> Lines;
    public DateTime? GlPostingDate;
    public DateTime TransactionDate;
    public string CustomerId;
}

public abstract class AbstractArAdjustmentLine : IXmlObject
{
    public string WarehouseId;
    public string EmployeeId;
    protected AbstractArAdjustmentLine();     
}

// 创建 ArAdjustmentCreate 实例

ArAdjustmentCreate arAdjustmentCreate = new ArAdjustmentCreate()
            {                
                CustomerId = "23",
                TransactionDate = DateTime.Now,
                GlPostingDate = DateTime.Now,                    
            };    

AbstractArAdjustmentLine arAdjustmentLine = null;                            
arAdjustmentLine.WarehouseId = "788"; // getting error Object reference not set
arAdjustmentLine.EmployeeId = "100";            
arAdjustmentCreate.Lines.Add(arAdjustmentLine);

如何在 AbstractArAdjustmentLine 摘要 class 中设置值?

您设置了 AbstractArAdjustmentLine arAdjustmentLine = null;,可能是因为您意识到无法实例化抽象 class,但您需要一个实例来设置属性。使用抽象 class 的唯一实用方法是继承子类型:

abstract class A { }
class B : A { }

// Works:
A a = new B();

// Works:
B b = new B();

// Does not work:
A c = new A();

从该站点查看 null keyword and abstract in the docs, and What is a NullReferenceException, and how do I fix it?