Design issue in C#: Generics or polymorphism or both? -


i need design issue i'm having. try achieve this:

i have main class called document. class has list of attribute classes. these attribute classes have common properties such identity , name, differ in 1 property call value. value type different each different attribute class , of type string, integer, list, datetime, float, list , classes consists of several properties. 1 example class call pairattribute have 2 properties: title , description.

what try achieve type safety value property of attribute child classes , these child classes should able added attribute list in document class. have made 1 attribute class have value property of type object , done it, try avoid here.

the common properties (identity , name) should placed in base class guess, lets call attributebase class. want have child class, stringattribute, value property of type string, integerattribute class value property of type integer, stringlistattribute value property of type list, pairattribute class value class several properties, etc

do know how can implement this? solution should go @ or better ways of solving type safety issue? appreciate code examples clarification:)

you may pass generic attribute instead of inheritance/method polymorphism, need store them in list , need interface, because datastores in .net cannot collections of undefined generic types, , if define them not able mix types inside:

    public interface iattribute {         string identity { get; set; }         string name { get; set; }         t getvalue<t>();     }      public class attribute<t> : iattribute     {          public string identity { get; set; }         public string name { get; set; }         public t value { get; set; }         public tret getvalue<tret>() {             return (tret)(object)value;         }     }      static class program     {         /// <summary>         /// main entry point application.         /// </summary>         [stathread]         static void main()         {             list<iattribute> lst = new list<iattribute>();             attribute<string> attr1 = new attribute<string>();             attr1.value = "test";              attribute<int> attr2 = new attribute<int>();             attr2.value = 2;              lst.add(attr1);             lst.add(attr2);              string attr1val = lst[0].getvalue<string>();             int attr2val = lst[1].getvalue<int>();          }     } 

the (tret)(object) not change type, boxes t , (tret) unboxes value without using middle variables. of course fail if miss right type when calling getvalue<type>(). if compatible types sent value integer , getvalue<double>() -> because unboxing integer double not allowed.

boxing/unboxing not fast casting ensures type safety preserved, , there @ knowledge no way use generics compile time known interface in other way.

so should type safe... , without lot of code.


Comments

Popular posts from this blog

objective c - Change font of selected text in UITextView -

php - Accessing POST data in Facebook cavas app -

c# - Getting control value when switching a view as part of a multiview -