Foreach - various instructions depending on the type of elements in the <FooBar> list
1 answer
Three options.
, Foo Bar FooBarBase - , FooBar - , DoStuff(). Foo Bar DoStuff().
, .
public interface FooBar {
void DoStuff(SomeFrameworkThing x);
}
...
List<FooBar> myFooAndBarList = new myFooAndBarList() { ... };
var thing = new SomeFrameworkThing(/* long list of murky parameters, all different */);
foreach (var fb in myFooAndBarList) {
fb.DoStuff(thing);
}
, , , (, ), . , , . # 7 , (1).
foreach (var o in myFooAndBarList) {
if (o is Foo) {
var f = o as Foo;
f.FooMethod();
f.FooProperty = "lol";
}
else if (o is Bar) {
var b = o as Bar;
b.BarMethod(234, 345);
b.BarProp = new Dictionary<Foo, List<Bar>>();
}
}
, , , , , dynamic:
public void DoStuff(Foo f) {
// stuff
}
public void DoStuff(Bar b) {
// other stuff
}
...
foreach (dynamic d in myFoAndBarList) {
DoStuff(d);
}
. . ; , , , , .
(1) , Visual Studio . "NO SYNTAX FOR YOU".
+5