0

I am trying to use system Reflection to enable extraction of a class member (type int)

var CurrTblID = typeof(currProjData).GetMembers(
                                  BindingFlags.Public |BindingFlags.Static
                                 ).Select(t => t.Name)...what expresssion should I put here?

this will expose an enumerator, with the interface iEnumerable and assign it to CurrTblID

so i could use foreach loop , to find which "table ID" Tid is the current one

  • How Can i get to a specific field name and find out its value without the foreach?

the Source for the question is in thecurrProjData class , shown below.

some background if needed for the answer :

in this code i am using a struct that enables setting Sql server Database table with details for later use.

i couldnt find answer for that in sql server so i've made a struct to hold a custom index - as a numeric ID for each table i will use in my application .

            public class DBMetaDetails
            {
                public struct DbTable
                {
                    public DbTable(string tableName, int tableId): this()
                    {
                        this.TableName = tableName;
                        this.TableID = tableId;
                    }

                    public string TableName { get;  set; }
                    public int TableID { get;  set; }
                }
            }

TableID is a custom numeric "id" i am using via that struct, so this is how i could reference the SQL table by a custom "index" rather its database name .

public static class currProjData 
{
    static DBMetaDetails.DbTable CustomersMeta = new DBMetaDetails.DbTable();
    static DBMetaDetails.DbTable TimesMeta = new DBMetaDetails.DbTable();

    public static void SetTablesMetaDetails()
    {

        TimesMeta.TableID = HTtIDs.TblTimes;
        TimesMeta.TableName = HTDB_Tables.TblTimes;

        CustomersMeta.TableID = HTtIDs.TblCustomers;
        CustomersMeta.TableName = HTDB_Tables.TblTimeCPAReport;

    }

    public static readonly int CustomersTid = CustomersMeta.TableID;
    public static readonly string CustomersTblName = CustomersMeta.TableName;

    public static readonly int TimesTid = TimesMeta.TableID;
    public static readonly string TimesTblName = TimesMeta.TableName;
}
LoneXcoder
  • 2,121
  • 6
  • 38
  • 76
  • Are you sure you want to get a field and not a property? See http://msdn.microsoft.com/en-us/library/kz0a8sxy.aspx for this. Furthermore, why do you want to use reflection? This seems to be a matter of plain OOP (casting to the according type and accessing the property). – Nico Schertler Dec 06 '12 at 15:50
  • @NicoSchertler i wanted to query some how . that is how i know to question about fields / members etc' , if there's a less expensive approach i will be happy to learn the implementation – LoneXcoder Dec 06 '12 at 15:52
  • @NicoSchertler target is : `public static readonly int TimesTid / CustomersTid ` extracting its value (currently assignd with an id=1 id=2...) . why ? i need to see which one of tables in data base is cuerently being fetched to be used in the application – LoneXcoder Dec 06 '12 at 16:01
  • Ok. Although I'm not convinced of the necessity of the reflection, you could use the `FirstOrDefault`-Statement OmegaMan suggested. But leave away the other code because that is not designed for static properties. – Nico Schertler Dec 06 '12 at 21:11

1 Answers1

1

How Can i get to a specific field name and find out its value without the foreach?

Create a generic extension Method which will handle any object and then internally do a FirstOrDefault:

public static int ExtractIntProperty<T>( this T targetItem, string propertyName )
{

   int result = 0;

   var prop = targetItem.GetType()
                        .GetProperties()
                        .FirstOrDefault( prp => prp.Name == propertyName );


   if (prop != null)
   {
      var val = prop.GetValue( targetItem, null );

      if (val != null)
         result = (int) val;
   }

   return result;

}

Then get the value as such (casting to show that it handles unknown objects)

object detail = (object) new DBMetaDetails.DbTable() { TableName = "Jabberwocky", TableID = 12 };

Console.WriteLine ( detail.ExtractIntProperty("TableID") ); // 12
ΩmegaMan
  • 29,542
  • 12
  • 100
  • 122
  • thank you so much OmegaMan , i really appreciate it , i think i can exercise on this some more to get the specified property which i have extracted its name(using your Ext' method) so how would you continue/evolve, from this `only - name getter` to a more practical one that will get a dictionary-like value (`name` vs `value`) output or only name as yours and only value just like yours but for its value as a co-operative set of extensions to be paired together in one class of same issue – LoneXcoder Dec 07 '12 at 04:43
  • oops i think i was out of sync here (L , your extension method is perfect ! sorry , it's late and i am jumping from one idea to another...for allmost 20hours straight... you dont want to know...didn't notes it's getting value not the property.name – LoneXcoder Dec 07 '12 at 04:55
  • funny thing is, as i was just spying on you a little, (out of respect for your skill.. good skill you've got) so anyways, the funny thing is, i went in to your website, straight to This http://omegacoder.com/?p=967 as it's currently at the main page... and it had somthing to do with my idea to cupple those values together – LoneXcoder Dec 07 '12 at 05:06
  • @LoneXcoder Thank you for your comments, I tried just to drill down to the main gist of what you needed and I am glad it worked. The post you mentioned [C#: Combine Two Values Together From a List Into Pairs](http://omegacoder.com/?p=967) was an *attempted* answer to an older Stack Overflow question that I never ended up posting to, but realized the code made a great article on the blog. Let me know if you need a updated answer to help with your idea. :-) – ΩmegaMan Dec 07 '12 at 16:04