gpt4 book ai didi

c# - 如何读取动态对象值

转载 作者:行者123 更新时间:2023-11-30 20:50:50 26 4
gpt4 key购买 nike

如何读取propertyValues属性?

    public void Create(object propertyValues)
{
//How to read propertyValues attributes?
//int code = propertyValues.Code;
//string name = propertyValues.ProductName;
}

public void CallMethod()
{
Create(new
{
Code = 100,
ProductName = "P1",
});
}

最佳答案

您需要将 object 参数更改为 dynamic 或创建您尝试传递的对象的强类型版本

public void Create(dynamic propertyValues)
{
int code = propertyValues.Code;
string name = propertyValues.ProductName;
}

public void Create(Order propertyValues)
{
//How to read propertyValues attributes?
var code = propertyValues.Code;
var name = propertyValues.ProductName;
}

public class Order{
public int Code {get; set;}
public string ProductName {get; set;}
}

public void CallMethod()
{
Create(new Order
{
Code = 100,
ProductName = "P1",
});
}

关于c# - 如何读取动态对象值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22274431/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com