as is exception oder exception as is usual oder doch Extensions
Allgemein C# .Net
Habe heute auf der Heimfahrt zufällig diesen Artikel gelesen -> warum-ich-as-nicht-mag
wobei das ... wohl die Kommentare sind
Kategorie Habe heute auf der Heimfahrt zufällig diesen Artikel gelesen -> warum-ich-as-nicht-mag
wobei das ... wohl die Kommentare sind
OK, zu Hause angekommen, mal schnell Visual
Studion aufgemacht und folgenden Code runtergeschrieben ->
namespace TestAsIs {
public interface I1 {
void I1Method();
}
public interface I2 {
void I2Method();
}
public class C1 : I1 {
public void I1Method() { System.Console.WriteLine("I1"); }
}
public class C2 : I2 {
public void I2Method() { System.Console.WriteLine("I2"); }
}
public class C3 : I1, I2 {
public void I1Method() { System.Console.WriteLine("3.I1"); }
public void I2Method() { System.Console.WriteLine("3.I2"); }
}
public static class Program {
static void Main(string[] args) {
object[] ao = new object[4];
ao[0] = new C1();
ao[1] = new C2();
ao[2] = new C3();
foreach (object o in ao) {
I1 c1 = o as I1;
I2 c2 = o as I2;
if (c1 != null) c1.I1Method();
if (c2 != null) c2.I2Method();
}
foreach(object o in ao) {
if (o is I1) ((I1)o).I1Method();
if (o is I2) ((I2)o).I2Method();
}
System.Console.ReadLine();
}
}
}
Zwei Interfaces, drei Klassen und gut ...... muß aber ehrlich sagen, keine der Implementierung gefällt mir so richtig.
Die erste mit dem as, ok aber lesbar ist es nicht.
I1 c1 = o as I1;
I2 c2 = o as I2;
if (c1 != null) c1.I1Method();
if (c2 != null) c2.I2Method();
Die zweite erscheint mir ein wenig kryptisch!
if (o is I1) ((I1)o).I1Method();
if (o is I2) ((I2)o).I2Method();
Ich persönlich würde mir einfach etwas wie folgt wünschen ->
o.I1Method();
o.I2Method();
das Schöne ist, man kann es durch die beiden Extension-Methods
public static void I1Method(this object o) {
if (o is I1) ((I1)o).I1Method();
}
public static void I2Method(this object o) {
if (o is I2) ((I2)o).I2Method();
}
erzielen.
Ob das ganze jetzt sinnvoll oder nicht, oder clean code oder nicht, dass sollen die Kommentatoren des anderen Blogs unter sich klären
Gruß JJR
P.S.: Was aber noch interessant ist -> mittels der Extension-Methods compiliert und läuft der folgende Code ->
C2 c = new C2();
c.I1Method();
c.I2Method();
Somit ist man vom "duck typing" auch im C# nicht mehr weit entfernt
namespace TestAsIs {
public interface I1 {
void I1Method();
}
public interface I2 {
void I2Method();
}
public class C1 : I1 {
public void I1Method() { System.Console.WriteLine("I1"); }
}
public class C2 : I2 {
public void I2Method() { System.Console.WriteLine("I2"); }
}
public class C3 : I1, I2 {
public void I1Method() { System.Console.WriteLine("3.I1"); }
public void I2Method() { System.Console.WriteLine("3.I2"); }
}
public static class Program {
static void Main(string[] args) {
object[] ao = new object[4];
ao[0] = new C1();
ao[1] = new C2();
ao[2] = new C3();
foreach (object o in ao) {
I1 c1 = o as I1;
I2 c2 = o as I2;
if (c1 != null) c1.I1Method();
if (c2 != null) c2.I2Method();
}
foreach(object o in ao) {
if (o is I1) ((I1)o).I1Method();
if (o is I2) ((I2)o).I2Method();
}
System.Console.ReadLine();
}
}
}
Zwei Interfaces, drei Klassen und gut ...... muß aber ehrlich sagen, keine der Implementierung gefällt mir so richtig.
Die erste mit dem as, ok aber lesbar ist es nicht.
I1 c1 = o as I1;
I2 c2 = o as I2;
if (c1 != null) c1.I1Method();
if (c2 != null) c2.I2Method();
Die zweite erscheint mir ein wenig kryptisch!
if (o is I1) ((I1)o).I1Method();
if (o is I2) ((I2)o).I2Method();
Ich persönlich würde mir einfach etwas wie folgt wünschen ->
o.I1Method();
o.I2Method();
das Schöne ist, man kann es durch die beiden Extension-Methods
public static void I1Method(this object o) {
if (o is I1) ((I1)o).I1Method();
}
public static void I2Method(this object o) {
if (o is I2) ((I2)o).I2Method();
}
erzielen.
Ob das ganze jetzt sinnvoll oder nicht, oder clean code oder nicht, dass sollen die Kommentatoren des anderen Blogs unter sich klären
Gruß JJR
P.S.: Was aber noch interessant ist -> mittels der Extension-Methods compiliert und läuft der folgende Code ->
C2 c = new C2();
c.I1Method();
c.I2Method();
Somit ist man vom "duck typing" auch im C# nicht mehr weit entfernt
Kommentare
Der erste Beispielcode aus dem genannten Blog...
DoSomething(a as B);
...
public void DoSomething(B b)
{
if (b == null)
{
throw new ArgumentNullException("b");
}
// ...
}
...ist in meinen Augen kein Plädoyer gegen die Nutzung von "as" sondern schon fast ein Abmahnungsgrund
Grüße,
Sebastian
P.S.: Die Lösung mit den Extension-Methods funktioniert zwar, sollte aber zur Sicherheit auch einen parental-advisory bekommen
Erstellt von Sebastian Stricker um 10:22:02 PM am 03/30/2011 | - Website - |
added as wished
Gruß JJR
Erstellt von JakeJBlues um 11:17:16 PM am 03/30/2011 | - Website - |