C# ClassA有多个Class B,如何从Class实例访问Class实例方法
C# ClassA has many ClassB, how to access ClassA instance method from ClassB instance
我的 C# 程序需要接收 xml 数据并将其存储在 sql 数据库中。我有很多指令的批次。我这样实现 classes:
class Batch
{
public string batchUID { get; set; }
public Instruction[] instructionArray { get; set; }
}
class Instruction
{
public string instructionUID { get; set; }
}
我的程序是这样运行的
Batch myBatch = new Batch();
myBatch.instructionArray = new Instruction[3];
myBatch.instructionArray[0] = new Instruction();
SomeFunc(myBatch.instructionArray[0]);
public void SomeFunc(Instruction instruction)
{
// How do I do the following?
console.write( instruction.OWNER.batchUID )
}
我试图广泛搜索这个,但每个结果都与继承、inner/outer classes 等有关。我想避免在 class 中创建 batchUID 方法可能的话请多多指教。
您的代码有问题:
myBatch.instructionArray = new Instruction[3];
这只会创建一个包含 3 个 null
值的数组,而不是 Instruction
的 3 个实例。这是因为 class
是一个引用类型,它不会在这里自动实例化(但是 struct
s 是,因为它们是值类型并在默认构造函数中初始化它们的所有值,然后调用).在将它们传递给您的方法之前,您仍然必须创建这些指令,因为您实际上只是在传递 null
.
然后,在创建Instruction
的实例时,只需将父级作为参数传递,并在class中记住它,就像这样:
class Instruction
{
public Instruction(Batch parent)
{
Parent = parent;
}
public Batch Parent { get; private set; }
public string InstructionUID { get; set; } // Please start properties with an uppercase letter
}
这样以后就可以访问Instruction
实例所属的Batch
实例了。
Batch myBatch = new Batch();
myBatch.InstructionArray = new Instruction[3];
// TODO: Create the instances here, the array is just null null null now!
myBatch.InstructionArray[0] = new Instance(myBatch); // example
SomeFunc(myBatch.InstructionArray[0]);
public void SomeFunc(Instruction instruction)
{
Console.Write(instruction.Parent.BatchUID)
}
如果这是好的设计是另一个问题,Instruction
本身可能包含 SomeFunc
内容,但我不确定该项目最终应该实现什么。
您可以将 Batch
的实例传递给 Instruction
的构造函数,然后存储对 Batch
的引用,您可以使用 属性 公开该引用。 (这已在另一个答案中显示 - 我重复它是因为它为我接下来要添加的内容提供了上下文。)
class Instruction
{
public Instruction(Batch parent)
{
Parent = parent;
}
public Batch Parent { get; private set; }
public string InstructionUID { get; set; }
}
现在 Instruction
有 Parent
属性,returns 有 Batch
。
但是有差距。如果你打电话
var parent = batch.Instruction[0].Parent
是parent == batch
吗?看起来就是这个意图。 Instruction
包含对包含它的 Batch
的引用。
但没有任何强制措施。例如,我可以这样做:
someBatch.Instruction[0] = someOtherBatch.Instruction[1];
现在 someBatch
包含 Instruction
的数组,但其中至少有一个 someOtherBatch
作为其 parent。
也许这种可能性没什么大不了的,但我认为如果 Parent
是指包含 Instruction
的 Batch
但它可能不是,那么你没有它实际上没有实现您的目标。
我建议创建一个包含 Batch
和 Instruction
的单独 class。 (也许 ParentInstructionRelation
?)
public class ParentInstructionRelation
{
Batch Parent {get;private set;}
Instruction Instruction {get;private set;}
public ParentInstructionRelation(Batch parent, Instruction instruction)
{
Parent = parent;
Instruction = instruction;
}
}
这样 Instruction
就不需要引用它的 parent。它可能不应该引用它的 parent。如果相同的 Instruction
在 Batch
的两个不同实例中怎么办? parent 是哪一个?
但是如果 Batch
需要公开一个 Instruction
和 对自身的引用,那么它可以通过返回一个 ParentInstructionRelation
来实现.或者从 Batch
读取 Instruction
的 class 可以创建该关系。
var instructions = batch.InstructionArray
.Select(instruction => new ParentInstructionRelation(batch, instruction));
我的 C# 程序需要接收 xml 数据并将其存储在 sql 数据库中。我有很多指令的批次。我这样实现 classes:
class Batch
{
public string batchUID { get; set; }
public Instruction[] instructionArray { get; set; }
}
class Instruction
{
public string instructionUID { get; set; }
}
我的程序是这样运行的
Batch myBatch = new Batch();
myBatch.instructionArray = new Instruction[3];
myBatch.instructionArray[0] = new Instruction();
SomeFunc(myBatch.instructionArray[0]);
public void SomeFunc(Instruction instruction)
{
// How do I do the following?
console.write( instruction.OWNER.batchUID )
}
我试图广泛搜索这个,但每个结果都与继承、inner/outer classes 等有关。我想避免在 class 中创建 batchUID 方法可能的话请多多指教。
您的代码有问题:
myBatch.instructionArray = new Instruction[3];
这只会创建一个包含 3 个 null
值的数组,而不是 Instruction
的 3 个实例。这是因为 class
是一个引用类型,它不会在这里自动实例化(但是 struct
s 是,因为它们是值类型并在默认构造函数中初始化它们的所有值,然后调用).在将它们传递给您的方法之前,您仍然必须创建这些指令,因为您实际上只是在传递 null
.
然后,在创建Instruction
的实例时,只需将父级作为参数传递,并在class中记住它,就像这样:
class Instruction
{
public Instruction(Batch parent)
{
Parent = parent;
}
public Batch Parent { get; private set; }
public string InstructionUID { get; set; } // Please start properties with an uppercase letter
}
这样以后就可以访问Instruction
实例所属的Batch
实例了。
Batch myBatch = new Batch();
myBatch.InstructionArray = new Instruction[3];
// TODO: Create the instances here, the array is just null null null now!
myBatch.InstructionArray[0] = new Instance(myBatch); // example
SomeFunc(myBatch.InstructionArray[0]);
public void SomeFunc(Instruction instruction)
{
Console.Write(instruction.Parent.BatchUID)
}
如果这是好的设计是另一个问题,Instruction
本身可能包含 SomeFunc
内容,但我不确定该项目最终应该实现什么。
您可以将 Batch
的实例传递给 Instruction
的构造函数,然后存储对 Batch
的引用,您可以使用 属性 公开该引用。 (这已在另一个答案中显示 - 我重复它是因为它为我接下来要添加的内容提供了上下文。)
class Instruction
{
public Instruction(Batch parent)
{
Parent = parent;
}
public Batch Parent { get; private set; }
public string InstructionUID { get; set; }
}
现在 Instruction
有 Parent
属性,returns 有 Batch
。
但是有差距。如果你打电话
var parent = batch.Instruction[0].Parent
是parent == batch
吗?看起来就是这个意图。 Instruction
包含对包含它的 Batch
的引用。
但没有任何强制措施。例如,我可以这样做:
someBatch.Instruction[0] = someOtherBatch.Instruction[1];
现在 someBatch
包含 Instruction
的数组,但其中至少有一个 someOtherBatch
作为其 parent。
也许这种可能性没什么大不了的,但我认为如果 Parent
是指包含 Instruction
的 Batch
但它可能不是,那么你没有它实际上没有实现您的目标。
我建议创建一个包含 Batch
和 Instruction
的单独 class。 (也许 ParentInstructionRelation
?)
public class ParentInstructionRelation
{
Batch Parent {get;private set;}
Instruction Instruction {get;private set;}
public ParentInstructionRelation(Batch parent, Instruction instruction)
{
Parent = parent;
Instruction = instruction;
}
}
这样 Instruction
就不需要引用它的 parent。它可能不应该引用它的 parent。如果相同的 Instruction
在 Batch
的两个不同实例中怎么办? parent 是哪一个?
但是如果 Batch
需要公开一个 Instruction
和 对自身的引用,那么它可以通过返回一个 ParentInstructionRelation
来实现.或者从 Batch
读取 Instruction
的 class 可以创建该关系。
var instructions = batch.InstructionArray
.Select(instruction => new ParentInstructionRelation(batch, instruction));