473,322 Members | 1,540 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

Problem with Reflections

Here is what I am trying to accomplish.
I have to be able to load the type from an object[] then find the property requested to be returned.

Sounds simple, and I can do it if I explictly cast the object but I need this to be dynamic as the items in the object[] could be of one type in one control but different in the next.

I am probably missing something but I keep getting this Error
"Object does not match target type."

Expand|Select|Wrap|Line Numbers
  1. Type dataItem = Parent.DataItems[0].GetType();
  2. PropertyInfo[] props = dataItem.GetProperties(); 
  3. PropertyInfo dn = dataItem.GetProperty("DisplayName");
  4. object myValue = dn.GetValue(dataItem, null); //<!--- this is the problem;
  5.  
Notice the object im requesting the value from is the same object that I am putting into the GetValue method, so it shouldnt be saying its not the same.

Everything I have tried has failed, I see the property in the PropertyInfo[] and I can return the Property I just cannot get the value to return (its a string value in this case)

Any help would be great!
Thanks,
R. Kozar
Oct 3 '09 #1
3 1323
Plater
7,872 Expert 4TB
You are not supplying any binding flags with the call:
PropertyInfo dn = dataItem.GetProperty("DisplayName");

Which usually means dn is ending up being null. Which would throw an exception when you try to get that property. Play around with the binding flags to make sure you are getting a valid property info into dn.


EDIT: Scratch the above, I misread your problem.

OK got it:
You are trying to execute the GetValue on the Type, not the object itself.
Change:
object myValue = dn.GetValue(dataItem, null);
to:
object myValue = dn.GetValue(Parent.DataItems[0], null);

Once you get everything working right, Reflection is a fantastic thing
Oct 5 '09 #2
Yup, that worked just the way I wanted, I knew it was something I was over looking. I thought that if I was assigning the type it would be the same as a direct reference.

Anyways, Thanks again.
R. Kozar
Oct 5 '09 #3
Plater
7,872 Expert 4TB
You would use the Type object to execute STATIC functions i believe, since you cannot use an instanciated class object for that
Oct 5 '09 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

17
by: Dave | last post by:
Hi I'm making a 3D Engine which consists of the class C3DEngine. Part of this engine is a file loader, a class called CMeshLoader. I have made an instance of CMeshLoader in C3DEngine, ie...
117
by: Peter Olcott | last post by:
www.halting-problem.com
28
by: Jon Davis | last post by:
If I have a class with a virtual method, and a child class that overrides the virtual method, and then I create an instance of the child class AS A base class... BaseClass bc = new ChildClass();...
2
by: Owen Hines | last post by:
If anyone can advise me of a way forward regarding the following it would be appreciated. I have a structure which is returned from a method that is called via reflections invokemember. when the...
7
by: Lacky | last post by:
Hi frends. I have very interesting problem. It's solution possibly not exist. Take a look. I need to create a method that can serialize any generic C# class (even when it's not implement...
6
by: Ammar | last post by:
Dear All, I'm facing a small problem. I have a portal web site, that contains articles, for each article, the end user can send a comment about the article. The problem is: I the comment length...
0
by: arturbl | last post by:
I just finished my sort of MVC/P framework where I do url rewriting. During the url rewriting I access properties of the controller/presenter based on the url. First thing that most of us would...
6
by: venuswilliams105 | last post by:
o) Does C++ have any security problems ? o) We'd like to put up our special tennis game online after we finish hard-coding and enforcing compiling error reduction. o) We are afraid the users would...
0
by: Robertson1995 | last post by:
I have been using Reflections for a while and I am familiar with macros, but am new to VBA. I need a VBA code that I think is fairly simple, but being new I need help. I have a piece of equipment...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.