I have an object “ConnectableProperty” that hooks up one property to another, and requires I feed it Func. Now I have 2 types at the moment – Scalar and Color. Both are castable to each other via explicit operators. For some reason I can’t feed a Func<double, double, Scalar> to Func<double, double Color>, even though Scalar can cast to Color. What’s the deal?
To clarify, i’ve added the code. Note that connectable properties are the “inputs”. The outputs (that can be plugged in) are Methods that have that signature.
Here’s ConnectableProperty
public sealed class ConnectableProperty<T> : IEquatable<T>, IGetXY<T> where T : IValue<T>
{
private T _value;
public T Value { get { return _value; } set { _value = value; } }
public INode ParentNode { get; private set; }
public ValueConnection<T> Connection { get; set; }
public INode ConnectionFrom { get { return !IsConnected ? null : Connection.FromNode; } }
public bool IsConnected { get { return Connection == null; } }
public ConnectableProperty(INode parentNode, T value)
{
ParentNode = parentNode;
_value = value;
}
public T GetXY(double x, double y)
{
return IsConnected
? Connection.FromValue(x, y)
: _value;
}
public void Connect(INode fromNode, Func<double, double, T> getXY)
{
Connection = new ValueConnection<T>(fromNode, ParentNode, getXY, this);
}
public void Disconnect()
{
Connection = null;
}
public bool Equals(T other)
{
return _value.Equals(other);
}
public static implicit operator T(ConnectableProperty<T> connectableProperty)
{
return connectableProperty._value;
}
}
And ValueConnection:
public class ValueConnection<T>
{
public INode FromNode { get; private set; }
public INode ToNode { get; private set; }
public Func<double, double, T> FromValue { get; private set; }
public ConnectableProperty<T> ToValue { get; private set; }
public ValueConnection(INode fromNode, INode toNode, Func<double, double, T> fromValue, ConnectableProperty<T> toValue)
{
// TODO: Implement INPC type thing
FromNode = fromNode;
ToNode = toNode;
FromValue = fromValue;
ToValue = toValue;
}
}
No, you won’t be able to perform that conversion directly, and I wouldn’t expect to be able to – but you could easily write a pair of methods to perform it for you:
When you’ve got a delegate which is meant to return a particular type, that has to be able to be invoked directly and return a value of the appropriate type. The caller shouldn’t have to know that even though they’ve got a
Func<X>, if they want to get anXout of it, they’ll need to cast the result to anX.