【发布时间】:2014-10-23 21:58:25
【问题描述】:
我需要在不知道具体类型的情况下查看泛型对象的属性:
foreach(var n in Nodes)
{
if(n.GetType().GetGenericTypeDefinition() == typeof(VariableNode<>))
{
if((n as VariableNode<>).Variable == myVar) //obviously this does not work
{
toRemove.Add(n);
}
}
}
那么,检查“变量”属性的最优雅的方法是什么? (变量是引用类型)
谢谢!
编辑:
节点定义:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UnityEngine;
using KSPComputer.Types;
using KSPComputer.Connectors;
namespace KSPComputer.Nodes
{
[Serializable]
public abstract class Node
{
public SVector2 Position;
public int InputCount
{
get
{
return inputs.Count;
}
}
public int OutputCount
{
get
{
return outputs.Count;
}
}
public FlightProgram Program { get; private set; }
private Dictionary<string, ConnectorIn> inputs;
private Dictionary<string, ConnectorOut> outputs;
public KeyValuePair<string, ConnectorIn>[] Inputs
{
get
{
return inputs.ToArray();
}
}
public KeyValuePair<string, ConnectorOut>[] Outputs
{
get
{
return outputs.ToArray();
}
}
public Node()
{
Position = new SVector2();
inputs = new Dictionary<string, ConnectorIn>();
outputs = new Dictionary<string, ConnectorOut>();
}
internal virtual void Init(FlightProgram program)
{
Program = program;
OnCreate();
}
protected void In<T>(string name, bool allowMultipleConnections = false)
{
var connector = new ConnectorIn(typeof(T), allowMultipleConnections);
connector.Init(this);
inputs.Add(name, connector);
}
protected void Out<T>(string name, bool allowMultipleConnections = true)
{
var connector = new ConnectorOut(typeof(T), allowMultipleConnections);
connector.Init(this);
outputs.Add(name, connector);
}
protected void Out(string name, object value)
{
ConnectorOut o;
if (outputs.TryGetValue(name, out o))
{
if (o.Connected)
{
o.SendData(value);
}
}
}
protected ConnectorOut GetOuput(string name, bool connected = true)
{
ConnectorOut o;
if (outputs.TryGetValue(name, out o))
{
if (o.Connected || !connected)
{
return o;
}
}
return null;
}
protected ConnectorIn In(string name)
{
ConnectorIn o;
if (inputs.TryGetValue(name, out o))
{
return o;
}
return null;
}
public void UpdateOutputData()
{
RequestInputUpdates();
OnUpdateOutputData();
}
protected virtual void OnUpdateOutputData()
{ }
protected virtual void OnCreate()
{ }
protected void RequestInputUpdates()
{
foreach (var i in inputs.Values)
{
i.FreshData = false;
}
foreach (var i in inputs.Values)
{
if (!i.FreshData)
{
i.RequestData();
}
}
}
public IEnumerable<Connector> GetConnectedConnectors()
{
return (from c in inputs.Values where c.Connected select c as Connector).Concat(from c in outputs.Values where c.Connected select c as Connector);
}
public IEnumerable<Connector> GetConnectedConnectorsIn()
{
return (from c in inputs.Values where c.Connected select c as Connector);
}
public IEnumerable<Connector> GetConnectedConnectorsOut()
{
return (from c in outputs.Values where c.Connected select c as Connector);
}
}
}
VariableNode的定义:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using KSPComputer;
using KSPComputer.Nodes;
using KSPComputer.Connectors;
using KSPComputer.Variables;
namespace KSPComputer.Nodes
{
[Serializable]
public class VariableNode<T> : ExecutableNode
{
internal Variable Variable { get; private set; }
internal void SetVariable(Variable variable)
{
this.Variable = variable;
}
protected override void OnCreate()
{
In<T>("Set");
Out<T>("Get");
}
protected override void OnExecute(ConnectorIn input)
{
Variable.Value = In("Set").Get<T>();
ExecuteNext();
}
protected override void OnUpdateOutputData()
{
Out("Get", Variable.Value);
}
}
}
【问题讨论】:
-
你能提供更多关于该代码的上下文吗?我会建议一个通用类型约束......但没有更多上下文我不确定这是否合适。
-
不幸的是,泛型类型约束不适用。你想看什么?
-
Node和VariableNode<>的定义会很好。
标签: c# generics types properties get