POLYMORPHISM ON C# PROPERTY

jayprakash sharma

Total Post:117

Points:821
Posted by  jayprakash sharma
C#
 1009  View(s)
Ratings:
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.

  1. Sumit Kesarwani

    Post:378

    Points:2694
    Re: Polymorphism on c# property

    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; }

    }

Answer

NEWSLETTER

Enter your email address here always to be updated. We promise not to spam!