从列表中获取类属性名称

时间:2013-12-01 11:51:12

标签: c# asp.net class

以下是我的问题:

我有一个这样的课程:

        class Artical
        {

            public int Id { get ; set;}
            public string Content-Language-en  { get ; set;}
            public string  Content-Language-de  { get ; set;}
            public string  Content-Language-ru  { get ; set;}
        }

    List<Artical> lst = new List<Artical>()
                         { new Artical{1,'en-content here ','','' },
                           new Artical{2,'russian content here','',''} };

我得到的会话值如下:Sesion["Lang"].ToString()

string Content-Language-en = Sesion["Lang"].ToString() ; //取决于用户选择的网页的lang。

我需要的是这样的事情:

foreach(var item in lst)
{

    if  (Content-Language-en ==(item property name)* ) 
    * I don't know how I can get the property name here!!
        {
         // do something
       }
        if  (Content-Language-ru ==(item property name)* ) 
    * I don't know how I can get the property name here!!
        {
         // do something
       }
}

2 个答案:

答案 0 :(得分:1)

我不完全确定你想要什么,但这是一个例子:

using System;
using System.Collections.Generic;
using System.Collections;
...
public class Artical:IEnumerable
{
    public Artical()
    {
    }

      public int Id { get; set;}
      public string Content_Language_en  { get ; set;}
      public string Content_Language_de  { get ; set;}
      public string Content_Language_ru  { get ; set;}

      public void ArticalCollection()
      {
          List<Artical> articalList = new List<Artical>();
      }


      public List<Artical> List { get; set; }

        IEnumerator IEnumerable.GetEnumerator()
      {
          return List.GetEnumerator();
      }

} 
...

//----------------------------------------------

List<Artical> lst = new List<Artical>()
            {
                         new Artical{
                         Id=1,
                         Content_Language_en="en-content here1",
                         Content_Language_ru="ru-content here1",
                         Content_Language_de="de-content here1"},

                         new Artical{
                         Id=2,
                         Content_Language_en="en-content here2",
                         Content_Language_ru="ru-content here2",
                         Content_Language_de="de-content here2"}

            };


        foreach(var item in lst)
        {
            Console.WriteLine("{0} {1} {2} {3}", 
                item.Id, item.Content_Language_en, item.Content_Language_ru, item.Content_Language_de);
        if  (item.Content_Language_en == "Hello in English" ) 

            {
             // do sometihng 
            }
            if  (item.Content_Language_ru =="Hello in Russian" ) 

            {
             // do sometihng 
            }

答案 1 :(得分:0)

这是解决方案,感谢此链接,Getting the name of a property in a list 我有点修改

 foreach (var item in lst)
                {

                foreach (var items in (item.GetType()).GetProperties())
                    {
                    Console.WriteLine(items.Name);
if(items.Name==Session["Dil"].toString()) { //do something}
                    }
                }