D.B
D.B

Reputation: 4289

Get properties values from an object generic list c#

i am trying to get the values from the properties of my generic list but i get an error "T does not contain a definition for...."

 var values GetValues(Id);

 if (values != null)
        {
            CreateTable<Object>(values);
        }

/////
    private void CreateTable<T>(IList<T> array) 
    {

        foreach (item in array)
        {
          //Problem is here **** when trying to get item.tag
         var text = new TextBox(){ Text = item.Tag , ID = item.TagID.ToString() };

        }

    }

How can make it work with generics? Appreciate any help

Upvotes: 0

Views: 330

Answers (1)

Curtis Lusmore
Curtis Lusmore

Reputation: 1872

Why is it that you expect that an object of some arbitrary T type has a Tag and TagID property? Where are these properties defined? If they are defined on an interface, let's say

public interface IItem
{
    string Tag { get; }
    int TagID { get; }
}

then you don't need generics, you can redefine CreateTable as

private void CreateTable(IList<IITem> array)
{
    foreach (var item in array)
    {
        //Problem is here **** when trying to get item.tag
        var text = new TextBox(){ Text = item.Tag , ID = item.TagID.ToString() };
    }
}

Upvotes: 1

Related Questions