如何循环类公共字符串并获取名称和值集 [英] How do I loop class public strings and get the name and value set

查看:67
本文介绍了如何循环类公共字符串并获取名称和值集的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

public class Itcr
{
			public string _var_TransactionType;
			public string _var_SPOEID;


			[DataMember]
			public string var_TransactionType
			{
			get { return _var_TransactionType; }
			set { _var_TransactionType = value; }
			}

			[DataMember]
			public string var_SPOEID
			{
			get { return _var_SPOEID; }
			set { _var_SPOEID = value; }
			}
}






www.helixpoint.com



www.helixpoint.com

推荐答案

我真的不明白你的问题。您想获取类型的公共字符串属性吗?如果是这样,那么反思就是第一选择。 但是,如果在编译时无法确定类型,则应该只执行此操作。

I don't really understand your question. Do you want to get the public string properties of a type? If so then reflection would be the first choice.  But you should really only do this if you cannot determine the type at compile time.

var type = someValue.GetType();
foreach (var prop in type.GetProperties())
{
   if (prop.PropertyType == typeof(string)
   {
      Console.WriteLine(prop.GetValue(someValue));
   };
};

如果您事先已经知道类型,那么简单的属性引用就可以了。

If you already know the type in advance then simple property referencing is fine.

var value = new Itcr();
...

Console.WriteLine(value.var_TranscriptType);
Console.WriteLine(value.var_SPOEID);

Michael Taylor

http://www.michaeltaylorp3.net

Michael Taylor
http://www.michaeltaylorp3.net


这篇关于如何循环类公共字符串并获取名称和值集的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆