Saturday 15 August 2015

c# - Get Property of generic type object without knowing the generic type -



c# - Get Property of generic type object without knowing the generic type -

i need see property of object of generic type, without knowing specific type:

foreach(var n in nodes) { if(n.gettype().getgenerictypedefinition() == typeof(variablenode<>)) { if((n variablenode<>).variable == myvar) //obviously not work { toremove.add(n); } } }

so, elegant way check property "variable" ? (variable reference type)

thanks!

edit:

def of node:

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 { { homecoming inputs.count; } } public int outputcount { { homecoming outputs.count; } } public flightprogram programme { get; private set; } private dictionary<string, connectorin> inputs; private dictionary<string, connectorout> outputs; public keyvaluepair<string, connectorin>[] inputs { { homecoming inputs.toarray(); } } public keyvaluepair<string, connectorout>[] outputs { { homecoming outputs.toarray(); } } public node() { position = new svector2(); inputs = new dictionary<string, connectorin>(); outputs = new dictionary<string, connectorout>(); } internal virtual void init(flightprogram program) { programme = 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) { homecoming o; } } homecoming null; } protected connectorin in(string name) { connectorin o; if (inputs.trygetvalue(name, out o)) { homecoming o; } homecoming null; } public void updateoutputdata() { requestinputupdates(); onupdateoutputdata(); } protected virtual void onupdateoutputdata() { } protected virtual void oncreate() { } protected void requestinputupdates() { foreach (var in inputs.values) { i.freshdata = false; } foreach (var in inputs.values) { if (!i.freshdata) { i.requestdata(); } } } public ienumerable<connector> getconnectedconnectors() { homecoming (from c in inputs.values c.connected select c connector).concat(from c in outputs.values c.connected select c connector); } public ienumerable<connector> getconnectedconnectorsin() { homecoming (from c in inputs.values c.connected select c connector); } public ienumerable<connector> getconnectedconnectorsout() { homecoming (from c in outputs.values c.connected select c connector); } } }

definition of 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); } } }

it looks should able utilize reflection:

foreach(var n in nodes) { if(n.gettype().getgenerictypedefinition() == typeof(variablenode<>)) { if(n.gettype().getproperty("variable").getvalue(n, null) == myvar) { toremove.add(n); } } }

c# generics types properties get

No comments:

Post a Comment