gpt4 book ai didi

c# - 只返回大对象的某些属性?

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

我有一个由许多不同属性组成的大对象。我正在使用四种不同的函数来操纵这个对象。每个函数返回对象。这是我的设置的一个非常简短的版本:

public class myproject
{
//many attributes
}

public myproject FunctionOne()
{
//do some logic.
//return myproject but only properties 4,5,6
}
public myproject FunctionTwo()
{
//do some logic.
//return myproject but only properties 1,2,3
}
public myproject FunctionThree()
{
//do some logic.
//return myproject but only properties 1,4,7
}
public myproject FunctionFour()
{
//do some logic.
//return myproject but only properties 3,2,5
}

所以问题是,当我返回我的对象​​时,我只想在该返回中提供某些属性。有点像我要返回一个自定义对象,该对象只是为此目的而动态创建的。

我有什么选择来完成这个?

最佳答案

如果您使用的是 REST 并返回 JSON,您不妨这样做:

WebApi 调用:

public HttpResponseMessage GetOne()
{
return this.Request.CreateResponse(
HttpStatusCode.OK,
FunctionOne());
}
public HttpResponseMessage GetTwo()
{
return this.Request.CreateResponse(
HttpStatusCode.OK,
FunctionTwo());
}

逻辑:

public dynamic FunctionOne()
{
//do some logic.
//return myproject but only properties 4,5,6
return new {property4 = "abc", property5 = "xyz", property6 = "123"};
}
public dynamic FunctionTwo()
{
//do some logic.
//return myproject but only properties 1,2,3
return new { property1 = "asdf", property2 = "123", property3 = "aaa" };
}

关于c# - 只返回大对象的某些属性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15957120/

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