Καλώς ορίσατε στο dotNETZone.gr - Σύνδεση | Εγγραφή | Βοήθεια
σε

 

Αρχική σελίδα Ιστολόγια Συζητήσεις Εκθέσεις Φωτογραφιών Αρχειοθήκες

Generics and Inheritance

Îåêßíçóå áðü ôï ìÝëïò Dimitris Papadimitriou. Τελευταία δημοσίευση από το μέλος γιωργος μπακογιαννης στις 16-11-2006, 19:18. Υπάρχουν 6 απαντήσεις.
Ταξινόμηση Δημοσιεύσεων: Προηγούμενο Επόμενο
  •  16-11-2006, 17:16 20479

    Generics and Inheritance

    Συνημμένα: GenericsAndCasting.zip
    Μπορεί κανείς να μου βρει γιατί δεν δουλεύει το παρακάτω σενάριο; (συμπολεμιστές vbάδες, sorry για την C#... οι κακιές παρέες βλέπετε!!! Το σενάριο θα ήταν ακριβώς το ίδιο και σε vb)
    Ορίστε το σενάριο:

    abstract class BaseClass<MyType>
    abstract class BaseType
    class CustomClass: BaseClass<CustomType>
    class CustomType: BaseType


    Το σενάριο είναι ok, αλλά το παρακάτω βγάζει invalid cast:

    public void Test()
    {
          CustomClass testInstance = new CustomClass();
          BaseClass<BaseType> testInstance2 = (BaseClass<BaseType>)testInstance;
    }


    Για να κάνετε δοκιμές κατεβάστε την επισύναψη...

    Dimitris Papadimitriou
    Software Development Professional
    dotNETZone.gr News

    Οι απαντήσεις παρέχονται για συγκεκριμένες ερωτήσεις και χωρίς καμιά εγγύηση. Διαβάστε επίσης τους όρους χρήσης.
  •  16-11-2006, 17:23 20480 σε απάντηση της 20479

    Απ: Generics and Inheritance

    Διόρθωση! Η πρώτη γραμμή είναι αυτή:
    abstract class BaseClass<MyType> where MyType : BaseType

    Dimitris Papadimitriou
    Software Development Professional
    dotNETZone.gr News

    Οι απαντήσεις παρέχονται για συγκεκριμένες ερωτήσεις και χωρίς καμιά εγγύηση. Διαβάστε επίσης τους όρους χρήσης.
  •  16-11-2006, 17:53 20483 σε απάντηση της 20480

    Απ: Generics and Inheritance

    Generics and Casting

    The C# compiler only lets you implicitly cast generic type parameters to Object, or to constraint-specified types, as shown in Code block 5. Such implicit casting is type safe because any incompatibility is discovered at compile-time.

    Code block 5. Implicit casting of generic type parameters

    interface ISomeInterface

    {...}

    class BaseClass

    {...}

    class MyClass where T : BaseClass,ISomeInterface

    {

    void SomeMethod(T t)

    {

    ISomeInterface obj1 = t;

    BaseClass obj2 = t;

    object obj3 = t;

    }

    }

    The compiler lets you explicitly cast generic type parameters to any other interface, but not to a class:

    interface ISomeInterface

    {...}

    class SomeClass

    {...}

    class MyClass

    {

    void SomeMethod(T t)

    {

    ISomeInterface obj1 = (ISomeInterface)t;//Compiles

    SomeClass obj2 = (SomeClass)t; //Does not compile

    }

    }

    However, you can force a cast from a generic type parameter to any other type using a temporary Object variable:

    class SomeClass

    {...}

    class MyClass

    {



    void SomeMethod(T t)



    {

    object temp = t;

    SomeClass obj = (SomeClass)temp;



    }

    }

    Needless to say, such explicit casting is dangerous because it may throw an exception at run time if the type argument used instead of the generic type parameter does not derive from the type to which you explicitly cast. Instead of risking a casting exception, a better approach is to use the is and as operators, as shown in Code block 6. The is operator returns true if the generic type parameter is of the queried type, and as will perform a cast if the types are compatible, and will return null otherwise. You can use is and as on both generic type parameters and on generic classes with specific type arguments.

  •  16-11-2006, 18:04 20485 σε απάντηση της 20480

    Απ: Generics and Inheritance

    Αυτό σου κάνει ?

     

    internal abstract class BaseClass<MyType> where MyType : BaseType
        {
            public abstract int GetValue(MyType value);
        }
        internal class CustomClass : BaseClass<BaseType>
        {
            public override int GetValue(BaseType value) {
                return value.GetValue();
            }
        }
        internal abstract class BaseType
        {
            public abstract int GetValue();
        }
        internal class CustomType : BaseType
        {
            public override int GetValue() {
                return 1;
            }
        }
        internal class TestClass
        {
            public void Test() {
                BaseClass<BaseType> baseClass = new CustomClass();
            }
        }


    while (!dead) learn();
  •  16-11-2006, 18:29 20486 σε απάντηση της 20485

    Απ: Generics and Inheritance

    Καλησπέρα Δημήτρη,

    το θέμα το έχουμε ξανασυζητήσει σε παλαιότερο thread...

    έχει να κάνει με το ζήτημα ότι δεν υποστηρίζονται covariant and contravariant generic types
    Palladinos Nick
    Software Engineer
    -----------------------
    The limits of my language mean the limits of my world. (Ludwig Wittgenstein)
  •  16-11-2006, 18:36 20487 σε απάντηση της 20485

    Απ: Generics and Inheritance

    (#*&%)(@#*&%@##$!! θα μας φάει το casting!

    @ baburanos : είναι η μόνη λύση που δουλεύει, αλλά θέλω να αποφύγω να κάνω casting και να μετατρέπω την basetype σε customtype.

    Ρε γ...ω. Αφού υπάρχει το customtype είναι derived από το basetype... γιατί να μην δουλεύει!

    Join the petition!

    "covariant and contravariant generic types in .net now!!!"

    "covariant and contravariant generic types in .net now!!!"

    "covariant and contravariant generic types in .net now!!!"

    (...τραγουδιέται όμως το "δεν υποχωρούμε αν δεν δικαιωθούμε"!!!)
    Dimitris Papadimitriou
    Software Development Professional
    dotNETZone.gr News

    Οι απαντήσεις παρέχονται για συγκεκριμένες ερωτήσεις και χωρίς καμιά εγγύηση. Διαβάστε επίσης τους όρους χρήσης.
  •  16-11-2006, 19:18 20500 σε απάντηση της 20487

    Απ: Generics and Inheritance

    Smile
    while (!dead) learn();
Προβολή Τροφοδοσίας RSS με μορφή XML
Με χρήση του Community Server (Commercial Edition), από την Telligent Systems