我有一个我正在创建的对象和该对象的函数,以将数据加载到各种属性中.虽然该方法可以正常工作,但我觉得它可能是多余的.这可以用更简洁或更好的方式完成吗?
var user = {
productLine: {
userActiveValue: []
},
id: {
PACT: null,
EDIPI: null,
AKO: null,
},
name: {
first: null,
last: null,
},
DMIS: null,
region: null,
email: null,
load: true,
loadUser: function (userInfoAPIResponse) {
this.id.PACT = userInfoAPIResponse.UID;
this.id.EDIPI = userInfoAPIResponse.EDIPN;
this.id.AKO = userInfoAPIResponse.akoUserID;
this.name.first = userInfoAPIResponse.fName;
this.name.last = userInfoAPIResponse.lName;
this.DMIS = userInfoAPIResponse.dmisID;
this.region = userInfoAPIResponse.RHCName;
this.email = userInfoAPIResponse.userEmail;
console.log(this);
}
};
Run Code Online (Sandbox Code Playgroud) 我来自JavaScript并试图更好地理解在C#中使用对象.我有一些简单的代码:
class Program
{
static void Main(string[] args)
{
MyClass firstObj = new MyClass();
firstObj.Number = 6;
Console.WriteLine(firstObj.Number);
object secondObj = doResults();
string getString = secondObj.GetType().GetProperty("resultsString").GetValue(secondObj).ToString();
Console.WriteLine(getString);
Console.ReadLine();
}
public static object doResults()
{
ResultsObj newResults = new ResultsObj();
newResults.resultsString = "Here is my string";
return newResults;
}
}
public class MyClass
{
public int Number { get; set; }
}
public class ResultsObj
{
public string resultsString { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
在Main()的第一部分中,我创建了一个对象,添加了一个属性,设置了一个值,然后访问并显示了该值.这一切都很直接.
在Main()的第二部分中,我调用doResults()方法,该方法创建一个字符串属性对象.
我的问题是这一行:
string getString = secondObj.GetType().GetProperty("resultsString").GetValue(secondObj).ToString(); …Run Code Online (Sandbox Code Playgroud)