你可以用至少3种方式做到这一点: 1.使用一个接受所有属性的构造函数 2.使用反射来迭代所有属性并设置它们的值 3.使用构造函数接受具有属性名称 的Hastable,然后使用反射来设置值。 让我知道我是否可以提供更多帮助。 HTH, Rakesh Rajan " einar"写道: 我有一个情况,我不知道我想做什么是可能的。 我有一个名为Account的课程属性和我为这些属性设置值。 public void foo() { account.BillingCity =" Wichita"; account.BillingCountry =" US"; account.BillingState =" KA" ;; //更多属性 account.create(); } 是否可以做这样的事情 public void bar() { string [] propName = {BillingCity,BillingCountry,BillingState} for(int i = 0; i< propName.length; i ++) { account.propName [i] = someVal; } account.create(); } 我想要做的是拥有一些属性名称列表,并为那些名称设置该属性的值。 我不知道想要使用soem开关/盒子或if / else语句 I have aa situation and i don''t know if what i want to do is possible.I have a class named Account with some properties and i set values for theseproperties like this.public void foo(){account.BillingCity = "Wichita";account.BillingCountry = "US";account.BillingState = "KA";// some more propertiesaccount.create();}Is it possible to do something like thispublic void bar(){string[] propName = {BillingCity ,BillingCountry,BillingState }for( int i=0; i< propName.length; i++ ){account.propName[i] = someVal;}account.create();}what i want to do is to have some list of properties names and for thosenames set the value of that propertie.I do not want to use soem switch/case or if/else statements 解决方案Are you wishing to set all the properties at once or just some?Perhaps within your Account class you can have a constructor thataccepts all properties and others for the most commonly set propertiesupon initial creation of an Account -- like the 3 you mentioned.public class Account{private string City, State, Country;public Account(string city, string state, string country){this.City = city;this.State = state;this.Country = country;// othercode}public Account(){// other code}// other code, perhap get/set accessors} // End of class Account..............then calling it..............public void foo(){// other codeAccount account = new Account("Wichita", "KA", "US");// other code}Or am I off base on your question?MsJuLiEOn Thu, 7 Oct 2004 03:13:01 -0700, "einar"<ei***@discussions.microsoft.com> wrote: I have aa situation and i don''t know if what i want to do is possible.I have a class named Account with some properties and i set values for theseproperties like this.public void foo(){ account.BillingCity = "Wichita"; account.BillingCountry = "US"; account.BillingState = "KA"; // some more properties account.create();}Is it possible to do something like thispublic void bar(){ string[] propName = {BillingCity ,BillingCountry,BillingState } for( int i=0; i< propName.length; i++ ) { account.propName[i] = someVal; } account.create();}what i want to do is to have some list of properties names and for thosenames set the value of that propertie.I do not want to use soem switch/case or if/else statements einar <ei***@discussions.microsoft.com> wrote: I have aa situation and i don''t know if what i want to do is possible. I have a class named Account with some properties and i set values for these properties like this. public void foo() { account.BillingCity = "Wichita"; account.BillingCountry = "US"; account.BillingState = "KA"; // some more properties account.create(); } Is it possible to do something like this public void bar() { string[] propName = {BillingCity ,BillingCountry,BillingState } for( int i=0; i< propName.length; i++ ) { account.propName[i] = someVal; } account.create(); } what i want to do is to have some list of properties names and for those names set the value of that propertie. I do not want to use soem switch/case or if/else statementsWell, you could use reflection to do that - use Type.GetProperty andPropertyInfo.SetValue. You lose type safety though, and performancewill go down.--Jon Skeet - <sk***@pobox.com> http://www.pobox.com/~skeetIf replying to the group, please do not mail me tooHi einar,You can do this in at least 3 ways:1. use a constructor which would accept all the properties2. use reflection to iterate thru all the properties and set their values3. use a constructor which would accept a Hastable with the property namesand then use reflection to set the values.Let me know if I could be of more help.HTH,Rakesh Rajan"einar" wrote: I have aa situation and i don''t know if what i want to do is possible. I have a class named Account with some properties and i set values for these properties like this. public void foo() { account.BillingCity = "Wichita"; account.BillingCountry = "US"; account.BillingState = "KA"; // some more properties account.create(); } Is it possible to do something like this public void bar() { string[] propName = {BillingCity ,BillingCountry,BillingState } for( int i=0; i< propName.length; i++ ) { account.propName[i] = someVal; } account.create(); } what i want to do is to have some list of properties names and for those names set the value of that propertie. I do not want to use soem switch/case or if/else statements 这篇关于C#语言问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
08-21 19:04