1

First of all, it's my first post here so sorry if it's wrong or anything just warn me it will be better next time.

Second I speak french x).

Ok it's not a problem but I would like to have a better way to acces my data who are stored in a mother class. I know i'm not clear let me show you maybe you will understand.

Main.cs :

namespace Xahor
{
    class Program
    {

        static void Main(string[] args)
        {
            TProduct test = new TProduct();

            test.FillData(1, "Apple", "4857", "A tasty apple.", "Green", false, true);

            test.PrintData();
        }
    }
}

TListNF.cs :

namespace Xahor
{
    public class TListNF
    {
        public TListNF(int iProductID = 0, string sProductName = "", string sProductIDNum = "", string sProductDesc = "", string sColor = "", bool bMake = false, bool bCustom = false)
        {
            this.iProductID = iProductID;
            this.sProductName = sProductName;
            this.sProductIDNum = sProductIDNum;
            this.sProductDesc = sProductDesc;
            this.sColor = sColor;
            this.bMake = bMake;
            this.bCustom = bCustom;
        }

        public int iProductID { get; set; }
        public string sProductName { get; set; }
        public string sProductIDNum { get; set; }
        public string sProductDesc { get; set; }
        public string sColor { get; set; }
        public bool bMake { get; set; }
        public bool bCustom { get; set; }

        protected List<TListNF> ItemList = new List<TListNF>();
    }
}

TProduct.cs :

namespace Xahor
{
    class TProduct : TListNF
    {
        public void FillData(int iProductID, string sProductName, string sProductIDNum, string sProductDesc, string sColor, bool bMake, bool bCustom)
        {
            ItemList.Add(new TListNF(iProductID, sProductName, sProductIDNum, sProductDesc, sColor, bMake, bCustom));
        }

        public void PrintData()
        {
            foreach (TListNF t in ItemList)
            {
                //Here where * is each of the accessor
                Console.WriteLine(t.*.ToString()); 
                Console.WriteLine(t.*.ToString()); 
                ...
            }
        }
    }
}

So, basicly what I don't know how to do is to get an easier acces to the getter what it would normally be a foreach so each time we enter the loop the var t get the value

Resolved

@Nair

Thank you I've figured out with the post

How to loop through all the properties of a class?

But your answer help by the way if anyone else need somethings like this I,ve used

foreach (PropertyInfo p in list.GetType().GetProperties())
                {
                    Console.WriteLine(p.Name + " : " + p.GetValue(list));
                }
//Where list is the List<ClassName_With_Accesor> list;
Community
  • 1
  • 1
Mokmeuh
  • 865
  • 3
  • 11
  • 25
  • I think he does not want to write 20 times the "Console.WriteLine(t.PropXXXX.ToString())` and he anticipates some way to "foreach over properties". Is that it Mokmeuh? – quetzalcoatl Jun 19 '13 at 10:50
  • @quetzalcoatl - if that is the case then this is a duplicate because that question has been answered many times before. – Hogan Jun 19 '13 at 10:52
  • @Hogan - yes, I was looking for the correct duplink right now.. He just joined SO, so a dup is very probable, but maybe let him confirm first ;) – quetzalcoatl Jun 19 '13 at 10:53
  • Yea this is what I don't want to write it 20 time for each set/get and as I said I don't know where to search if you could direct me I would probably find it but I don't know what keyword I can use – Mokmeuh Jun 19 '13 at 10:54
  • possible duplicate of [How to loop through all the properties of a class?](http://stackoverflow.com/questions/531384/how-to-loop-through-all-the-properties-of-a-class) – quetzalcoatl Jun 19 '13 at 10:54
  • Please look at the linked article and its solution. It has all that's needed to "loop over properties of a class". You'll get a PropertyInfo object for each property, and with the `GetValue` method you will be able to read all values from all properties. – quetzalcoatl Jun 19 '13 at 10:56
  • I would split it into two possibilities. Either you need the access by property name all the time---this means that you actually need to properties, but just `Dictionary` or whatever. Or you need to go through all the properties just in output routine, so it's better to list there the properties just once. – Vlad Jun 19 '13 at 11:02

2 Answers2

0

You can achieve it through reflecting individual properties in the type. You may need to add more constructive logic as the intention is to show the idea.

   foreach (TListNF t in ItemList)
  {
   foreach (PropertyInfo proInfo in t.GetType().GetProperties())
      {
       Console.WriteLine(proInfo.GetGetMethod().Invoke(t,null).ToString());
      }
  }
S.N
  • 4,910
  • 5
  • 31
  • 51
0

Of course the answer @Nair is correct. But usually it's bad practice to use reflection to for easy purposes. This is because of access rights of your application and so on. Here two other options you might try.

Option 1: Yield the properties in an own method (see TListNF.GetItemInfo). Disadvantage: You have to add another yield it in the implementation of GetItemInfo everytime you add another property to your TListNF class. Advantage: No use of reflection at all.

Option 2: Use an own attribute (see MyInfoAttribute) to mark the properties that are of interest to you. Usually classes do have several more attribute that you don't want to print. Advantage: Only marked attributes are printed: Disadvantage: Use of reflection.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;

namespace ConsoleApplication1
{
    class Program
    {
        [AttributeUsage(AttributeTargets.Property)]
        public class MyInfoAttribute : Attribute
        {
        }

        public class TListNF
        {
            public TListNF(int iProductID = 0, string sProductName = "", string sProductIDNum = "", string sProductDesc = "", string sColor = "", bool bMake = false, bool bCustom = false)
            {
                this.iProductID = iProductID;
                this.sProductName = sProductName;
                this.sProductIDNum = sProductIDNum;
                this.sProductDesc = sProductDesc;
                this.sColor = sColor;
                this.bMake = bMake;
                this.bCustom = bCustom;
            }

            [MyInfo]
            public int iProductID { get; set; }
            [MyInfo]
            public string sProductName { get; set; }
            [MyInfo]
            public string sProductIDNum { get; set; }
            [MyInfo]
            public string sProductDesc { get; set; }
            [MyInfo]
            public string sColor { get; set; }
            [MyInfo]
            public bool bMake { get; set; }
            [MyInfo]
            public bool bCustom { get; set; }

            protected List<TListNF> ItemList = new List<TListNF>();

            public IEnumerable<string> GetItemInfo()
            {
                yield return iProductID.ToString();
                yield return sProductName;
                yield return sProductIDNum;
                yield return sProductDesc;
                yield return sColor;
                yield return bMake.ToString();
                yield return bCustom.ToString();
            }
        }

        class TProduct : TListNF
        {
            public void FillData(int iProductID, string sProductName, string sProductIDNum, string sProductDesc, string sColor, bool bMake, bool bCustom)
            {
                ItemList.Add(new TListNF(iProductID, sProductName, sProductIDNum, sProductDesc, sColor, bMake, bCustom));
            }

            public void PrintData()
            {
                foreach (TListNF item in ItemList)
                {
                    foreach (string info in item.GetItemInfo())
                        Console.WriteLine(info);
                }
            }

            public void PrintDataReflection()
            {
                Type type = typeof(MyInfoAttribute);

                foreach (TListNF item in ItemList)
                {
                    foreach (PropertyInfo proInfo in item.GetType().GetProperties().Where(p => p.GetCustomAttributes(type, true).Length > 0))
                    {
                        Console.WriteLine(proInfo.GetGetMethod().Invoke(item, null).ToString());
                    }
                }
            }
        }

        static void Main(string[] args)
        {
            var test = new TProduct();

            test.FillData(1, "Apple", "4857", "A tasty apple.", "Green", false, true);

            test.PrintData();
            test.PrintDataReflection();

            Console.ReadKey();
        }
    }
}
Matthias
  • 5,574
  • 8
  • 61
  • 121
  • Thank you these answer are also working but the list is huge I just posted a part of the code but thank you I know it now :P – Mokmeuh Jun 19 '13 at 12:20