Code: Select all
public class Node{
public Node NextNode{get; set;}
public static Node WithData(T data) => new Node(data);
}
public class Node: Node{
public T Data{get; set;}
public Node(T data) => Data = data;
}
Code: Select all
var node1 = Node.WithData(73); // the node contains an int
var node2 = Node.WithData("Good luck!"); // the node contains a string
node1.NextNode = node2; // chaining the nodes
Notify(node1.Data == 73)); // displays "true" (method Notify() omitted for brevity)
Notify(node1.NextNode.GetType()); // displays "Node`1[System.String]"
Notify(node1.NextNode.Data == "Good luck!");
//compile error: 'Node' does not contain a definition for 'Data'
Ich kann das Problem lösen, indem ich zurück zu Node (Typprüfungen entfallen für Kürze):
Code: Select all
Notify(((Node)node.NextNode).Data);
// verbose and the casting type is usually not known at compile time
Code: Select all
Notify(((dynamic)node.NextNode).Data); // slow, but works fine at runtime
Code: Select all
if(node.NextNode is Node stringNode) Notify(stringNode.Data);
else if(node.NextNode is Node intNode) Notify(intNode.Data);
// else if... etc.
// bad for maintenance, verbose, especially with many data types
- Gibt es eine geeignetere Möglichkeit, Node auf Node herunterzuwandeln (vorzugsweise zur Laufzeit)?
< li>Gibt es einen besseren Ansatz oder ein besseres Design der beiden Klassen, um das zu vermeiden oder zu automatisieren? niedergeschlagen?