I have an abstract class as follows:
public abstract class Node
{
public abstract void run();
}
There are some child nodes that may have several properties as input and output. But all the operation is done just in run()
method that each Node should implement. For example a Node that draws a line could be something like this:
public class LineNode : Node
{
[Input]
public Point a;
[Input]
public Point b;
[Output]
public Line line;
public override void run()
{
line = new Line(a, b);
// draw line ...
}
}
[AttributeUsage(AttributeTargets.Field)]
public class Input : System.Attribute
{
}
[AttributeUsage(AttributeTargets.Field)]
public class Output : System.Attribute
{
}
As you see I don't have any prior information about fields in the child classes. ** For a Node to be a valid node in convention, It is just required to have at least one output.** I want to force all other users to have at least one output in their Nodes so I can find it by reflection by looking for a field that [Output] attribute is applied to.
Is this possible?
And if this is, Is it the best way?
Thanks for any help
Here the same issue raised but the suggested solution settled it in the run time. I was looking for an Object Oriented solution though. Which one is more logical actually?
How about using an Interface
IOutputDefinition
and defining an abstractICollection<IOutputDefinition>
in theNode
-class. All Implementers have to fill this in order to enable your code to determine which outputs the implementation will produce.You may define any Metadata-Properties like Name, Output-Type, ... or even a property containing a Getter-Expression for the Output in the Interface