I'm currently building a node editor (as in Blender) and am having trouble getting delegates to property accessors from a generic type. So far the question here has brought me closest, but I'm having trouble that I think is specifically related to the type of object being generic.
For reference, a "Node" is synonymous with an object, and a "Port" is synonymous with a property.
This is the offending code, which is part of the Node
class. The NodePort
class is an attribute that may be set on properties to provide details (such as the human-readable name, and data-flow direction) for the property.
public void SetTarget<T>(T Target)
{
//TODO: Finish clearing old IOs (if any)
Inputs.Clear();
Outputs.Clear();
//Keep track of the current target of this node.
ThisTarget = Target;
PropertyInfo[] pinfo = Target.GetType().GetProperties();
foreach (PropertyInfo property in pinfo)
{
Attribute[] attrs = Attribute.GetCustomAttributes(property);
foreach (Attribute attribute in attrs)
{
// If the property has a NodePort attribute, it's specifically requesting to be available as a port on the node.
if (attribute is NodePort)
{
NodePort PortDetails = (NodePort)attribute;
if (PortDetails.Direction == NodePort.NodePortDirection.PORT_INPUT)
{
// This line throws an ArgumentException, and the only message is "Error binding to target method."
NodeInput<T>.SetValue Setter = (NodeInput<T>.SetValue)Delegate.CreateDelegate(typeof(NodeInput<T>.SetValue), (T)Target, property.GetSetMethod());
AddInput(Setter, PortDetails.CommonName);
}
else if (PortDetails.Direction == NodePort.NodePortDirection.PORT_OUTPUT)
{
// Same exception here.
NodeOutput<T>.GetValue Getter = (NodeOutput<T>.GetValue)Delegate.CreateDelegate(typeof(NodeOutput<T>.GetValue), (T)Target, property.GetGetMethod());
AddOutput(Getter, PortDetails.CommonName);
}
}
}
}
}
NodeOutput<T>.GetValue
and NodeInput<T>.SetValue
are defined as such:
public delegate T GetValue();
public delegate void SetValue(T value);
...in NodeOutput
and NodeInput
respectively.
Does anyone have any experience with creating delegates for property accessors? Any idea how it may be different when the type in question is generic?