Click here to Skip to main content
15,911,531 members
Home / Discussions / C#
   

C#

 
QuestionAccessing property values using GetValue() Pin
mossu8-Oct-05 16:35
mossu8-Oct-05 16:35 
Hi,

I'm trying to build a test in nUnit, that checks if after a specific function call, all numeric property values are set to zero. Since I don't know if I will eventually add other properties, I tried building this test in a generic way. I'm not even sure I can do something like this, but if yes, how should I do it?

I'm used to programming in VB 6 and a bit of VB.Net, so I'm not too sure where to start on how to do this in C#, but I've tried a couple of things that look like this :
MyClass myClass = new MyClass();

for (int i = 0; i <= myClass.GetType().GetProperties().GetUpperBound(0);i++)
    {			
	if (myClass.GetType().GetProperties()[i].PropertyType.IsValueType)
	{
             if (myClass.GetType().GetProperties()[i].GetType().IsArray)
             {
	          MessageBox("Don't forget to check array properties eventually!");
             }
             else
             {

                  //Assert.AreEqual(0,(int)myClass.GetType().GetProperties()[i].GetValue(this, null));
                  Assert.AreEqual(0,(int)myClass.GetType().GetProperties()[i].GetValue(myClass.GetType().GetProperties()[i],null));
             }
        }
     }


Each time, I get a System.Reflection.TargetException.

Thanks in advance if anyone can help me!

P.S. Maybe it could be easyier to access the object's private variables directly, but I don't know how to do that either.

jG
AnswerRe: Accessing property values using GetValue() Pin
Guffa9-Oct-05 0:22
Guffa9-Oct-05 0:22 
GeneralRe: Accessing property values using GetValue() Pin
mossu9-Oct-05 2:38
mossu9-Oct-05 2:38 
GeneralRe: Accessing property values using GetValue() Pin
Guffa9-Oct-05 8:50
Guffa9-Oct-05 8:50 
GeneralRe: Accessing property values using GetValue() Pin
mossu10-Oct-05 3:01
mossu10-Oct-05 3:01 
AnswerRe: Accessing property values using GetValue() Pin
S. Senthil Kumar9-Oct-05 3:18
S. Senthil Kumar9-Oct-05 3:18 
GeneralRe: Accessing property values using GetValue() Pin
mossu9-Oct-05 6:05
mossu9-Oct-05 6:05 
QuestionOODBMS are the best solution for Today applications Pin
tolgayikilmaz8-Oct-05 12:16
tolgayikilmaz8-Oct-05 12:16 
AnswerRe: OODBMS are the best solution for Today applications Pin
johhnny8-Oct-05 12:20
johhnny8-Oct-05 12:20 
AnswerRe: OODBMS are the best solution for Today applications Pin
Colin Angus Mackay8-Oct-05 22:26
Colin Angus Mackay8-Oct-05 22:26 
GeneralRe: OODBMS are the best solution for Today applications Pin
tolgayikilmaz9-Oct-05 1:45
tolgayikilmaz9-Oct-05 1:45 
GeneralRe: OODBMS are the best solution for Today applications Pin
Colin Angus Mackay9-Oct-05 1:48
Colin Angus Mackay9-Oct-05 1:48 
AnswerRe: OODBMS are the best solution for Today applications Pin
Kevin McFarlane9-Oct-05 8:29
Kevin McFarlane9-Oct-05 8:29 
QuestionOpening the Form in maximized mode? Pin
E6AD8-Oct-05 12:09
E6AD8-Oct-05 12:09 
AnswerRe: Opening the Form in maximized mode? Pin
S. Senthil Kumar8-Oct-05 20:03
S. Senthil Kumar8-Oct-05 20:03 
QuestionWindowScraping Pin
budidharma8-Oct-05 11:17
budidharma8-Oct-05 11:17 
AnswerRe: WindowScraping Pin
XRaheemX10-Oct-05 5:06
XRaheemX10-Oct-05 5:06 
QuestionTaskbar Applications Pin
jgallen238-Oct-05 11:13
jgallen238-Oct-05 11:13 
QuestionRichTextBox Qeustion Pin
Mohammad A Gdeisat8-Oct-05 10:45
Mohammad A Gdeisat8-Oct-05 10:45 
AnswerRe: RichTextBox Qeustion Pin
mav.northwind9-Oct-05 0:31
mav.northwind9-Oct-05 0:31 
QuestionThreads Pin
ShimiG8-Oct-05 8:32
ShimiG8-Oct-05 8:32 
AnswerRe: Threads Pin
S. Senthil Kumar8-Oct-05 20:07
S. Senthil Kumar8-Oct-05 20:07 
GeneralRe: Threads Pin
ShimiG9-Oct-05 4:27
ShimiG9-Oct-05 4:27 
GeneralRe: Threads Pin
S. Senthil Kumar9-Oct-05 5:01
S. Senthil Kumar9-Oct-05 5:01 
GeneralRe: Threads Pin
ShimiG9-Oct-05 5:16
ShimiG9-Oct-05 5:16 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.