dimanche 19 juillet 2015

Suppose we have a NodeInput class like this:

public class NodeInput
{

}
public sealed class NodeInput<T> : NodeInput
{

}

And we have a Node class as follows:

public abstract class Node
{
    public Node()
    {
        var fieldInfos = GetType().GetRuntimeFields();
        // loop through all NodeInputs using their concrete base type
        foreach (var item in fieldInfos)
        {
            Type t = item.FieldType;
            if (t.BaseType == typeof(NodeInput))
            {
                // Here I want to initialize each NodeInput field according to
                // the T but I don't know what the T is here. 
            }
        }
    }
}

I have created the concrete parent class NodeInput so I can numerate all NodeInput<T> regardless of their T parameter type. But I also need the T to create the appropriate object and assign to each field in the loop. For an example PointNode in the following code should initiate all NodeInput fields with default(T) when we create an object of that:

 public class PointNode : Node
{
    public NodeInput<double> x;
    public NodeInput<int> y;
    public NodeInput<float> z;
}
PointNode node = new PointNode (); 
// Now x,y and z should be 0 = default(double),default(int),default(float)

Is that possible?





Aucun commentaire:

Enregistrer un commentaire