Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Get value from a dictionary created from a PropertyInfo.GetValue

Posted on 2014-03-06
3
Medium Priority
?
665 Views
Last Modified: 2014-03-07
How to get this to work?  

GSD a class that is used to store cache images of SQL tables. GSD has several public static properties representing different "CacheTables", which are Dictionary=long,rowtypeclass= objects, each with a different rowtype class.  The rowtype class objects model SQL table rows.  

   
public class GSDataObject
    {
		private Dictionary<long, GRPListRow> prvGRPList;
		private Dictionary<long, TestTableRow> prvTestTable;
		//=======================================
        public Dictionary<long, GRPListRow> GRPList
        {
            get { return prvGRPList;}
            set { prvGRPList = value; }
        }

		//=====================================
		public Dictionary<long, TestTableRow> TestTable
		{
			get { return prvTestTable; }
			set { prvTestTable = value; }
		}


    public class TestTableRow{

			public int ID  { get; set; }
			public string Field1  { get; set; }
			public string Field2  { get; set; }
			public string Field3  { get; set; }
			public string Field4  { get; set; }
			public string Field5  { get; set; }
			public string Field6  { get; set; }
			public string Field7  { get; set; }
			public string Field8  { get; set; }
    }

Open in new window

GSD and its different CacheTable properties work fine when declared hard-coded;  I want to access them with reflection.

Specifically, I want to get a particular Row from a particular CacheTable in an instance of GSD, update that row, and then put it back.  The instructions below describe the "get the row" phase.

The first three instructions work, and the resulting wrkCacheTableObject is of the correct type Dictionary=long,wrkRowtype=.  However, wrkCacheTableObject is not indexed, so I can't retrieve rows from it.

wrkGSD is an instantiation of a class GSD.  wrkCacheTableName is a string name of the particular CacheTable property. wrkRowType is the string class name of the row type.

   
wrkRow = Activator.CreateInstance(wrkRowType);
    PropertyInfo wrkTablePropInfo = wrkGSDOType.GetProperty(wrkCacheTableName);

    object wrkCacheTableObject = wrkTablePropInfo.GetValue(wrkGSD, null);  // <== gives correct CacheTable instance
    wrkTableDictObject = (Dictionary<long, object>)wrkCacheTableObject;  //<=== fails here

    wrkRow = wrkTableDictObject[wrkRowID];

    // update wrkRow fields using reflection //<== this works if I retrieve wrkRow via hard code
    // put it back into wrkTableDictObject
    // put wrkTableDictObject back into wrkGSD

Open in new window


I'm not fixed on this particular set of instructions, but I'd like something that will do basically the same thing, in not too many more lines of code.  And maybe if I can get the first phase above to work, it will show me how to do the other phases.

Any help with this would be appreciated.

Thanks!
0
Comment
Question by:codequest
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 16

Accepted Solution

by:
SriVaddadi earned 2000 total points
ID: 39912537
Try this code


var wrkTablePropInfo = wrkGSDOType.GetProperty(wrkCacheTableName);
            dynamic wrkCacheTableObject = wrkTablePropInfo.GetValue(wrkGSD, null);   // <== gives correct CacheTable instance
            var wrkRow = wrkCacheTableObject[0];
0
 
LVL 2

Author Comment

by:codequest
ID: 39914169
@SriVaddadi:   slight variation works:

dynamic wrkCacheTableObject = wrkTablePropInfo.GetValue(wrkGSD, null);
//--- get the row using dynamic
dynamic wrkRow = wrkCacheTableObject[(long)varAJR.rowID];
//--- put the row back
wrkCacheTableObject[(long)varAJR.rowID]= wrkRow;

Big help, thanks!
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…

609 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question