gpt4 book ai didi

c# - 将属性实现转发给成员属性的快速方法

转载 作者:行者123 更新时间:2023-11-30 16:48:47 25 4
gpt4 key购买 nike

我又花了一天时间编写锅炉代码,我需要找到一种更快的方法。并非所有接口(interface)实现都是微不足道的,但有一种情况对我来说似乎非常微不足道。

假设我有一个带有 getter 和 setter 的属性需要转发到也有 getter 和 setter 的成员属性。最快的写法是什么?

这是一个接近最小化的编译示例。在评论中查看我想写的内容。

//Core is unchangeable external code
class Core
{
public int prop { get; set; }
public Core() { prop = 42; }
}

//Boilerfun can be modified
interface IBoilderFun
{
int Prop { get; set; }
}

//This implements the adapter for the boiler special case Core
class CoreBoilerAdapter : IBoilderFun
{
private Core core;

public CoreBoilerAdapter(Core core)
{
this.core = core;
}

public int Prop
{
get { return core.prop; }
set { core.prop = value; }
}
//public int Prop { core.prop; } <-- this would be awsome
}

class Program
{
static void Main(string[] args)
{
IBoilderFun bf = InstanciateBasedOnConfig();
System.Console.WriteLine(bf.Prop);
}

private static IBoilderFun InstanciateBasedOnConfig()
{
return new CoreBoilerAdapter(new Core());
}
}

最佳答案

Resharper 有一个名为“生成委派成员”的功能。否则,您可能会花几个小时编写基于 Roslyn 的重构。

关于c# - 将属性实现转发给成员属性的快速方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37708253/

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