Home > DeveloperSection > Forums > Polymorphism on c# property
jayprakash sharma
jayprakash sharma

Total Post:117

Points:821
Posted on    August-28-2014 1:14 AM

 C#
Ratings:


 1 Reply(s)
 729  View(s)
Rate this:

I have a class like this:

public class ItemField

{

    public string FieldName { get; set; }

    public object FieldValue

    {

        get;

        set;

    }

}

I want to have it so that "FieldValue" can only be of type string, MyItem, or List<MyItem>. I also want it so that when code consumes this class, accessing "FieldValue" returns the right type of object. Is there a way to do this? I'm not too clear on generics, but there seems to be a way to do this using that.



Sumit Kesarwani

Total Post:378

Points:2694
Posted on    August-28-2014 2:11 AM

Hi jay, 
try this:

public class ItemField<T> where T :  ICloneable, IConvertible, IComparable<string>, IEnumerable<char>, IEnumerable, IEquatable<string>

{

    public string FieldName { get; set; }

    public T FieldValue { get; set; }

}


Don't want to miss updates? Please click the below button!

Follow MindStick