Polymorphism on c# property

Total Post:117

Points:821
C# 
C#
 1311  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. 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; }
    }

      Modified On Apr-05-2018 04:32:39 AM

Answer