开发者

A function that will convert input string to the actual object

I am unsure how to describe what I am looking for, 开发者_如何学运维so hopefully the situation will make it somewhat clear.

I have an object with a number of properties (let's say object.one, object.two, object.three). There are about 30 of these properties and they all hold a string ("Pass" or "Fail").

Right now the existing code checks whether the property has value "Pass" or "Fail" and then runs some code that prints stuff out. That is, the same snippet of code is duplicated 30 times, one for each of these properties.

The code looks something like this

If (object.one = ... )

...

End if

If (object.two = ... )

...

End if

If (object.three = ... )

...

End if

I want to use a loop to clean this mess up (each block is huge), but am not sure how to do it. I was thinking perhaps there was a way such that I might be able to construct a string like "object.one" and run some function that will tell the compiler that this is actually an object's property?

That way I could create an array containing the object's name like my array = {"object.one", "object.two", "object.three"} and then do something like, in pseudocode

For each string in my array

   If (some_function(string) = ...)

      ...

   End If

Essentially, it would take those massive blocks of duplicated code and reduce it to just one block. Is there such a some_function that I am looking for? This is in VB.net.


I'm not sure i've understand but you can use reflection and get object properties runtime ?

With reflection you can access public object properties, use PropertyFiled.GetValue() to get one, two, etc. and build an array (i suppose that one, two, etc are object Properties, true?)

Here you can find more information: http://msdn.microsoft.com/en-us/library/system.reflection.aspx

Sorry for my bad english, i'm italian.


You seem to be describing serialization, which is the act of converting object state to a format that can be stored/transmitted and deserialization, which is the opposite.

The .NET framework has several different serializers that can work with text - either XML or JSON - the DataContractSerializer for XML and the DataContractJsonSerizlizer for JSON amongst them.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜