Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/backbone.js/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用泛型委托或以其他方式减少代码_C#_.net_Delegates - Fatal编程技术网

C# 使用泛型委托或以其他方式减少代码

C# 使用泛型委托或以其他方式减少代码,c#,.net,delegates,C#,.net,Delegates,你好,你能帮我减少C语言中的代码吗?我有很多这样的函数 我想减少一个带有参数函数的函数的代码,稍后我将发送该函数 public void WriteTransportCurrectCategoryTypes() { var jStr = GetJSONString("GetBusTypes"); var jArr = JArray.Parse(jStr); var tTypes = new List<TransportCurr

你好,你能帮我减少C语言中的代码吗?我有很多这样的函数 我想减少一个带有参数函数的函数的代码,稍后我将发送该函数

    public void WriteTransportCurrectCategoryTypes()
    {
        var jStr = GetJSONString("GetBusTypes");
        var jArr = JArray.Parse(jStr);
        var tTypes = new List<TransportCurrentCategoryType>();
        foreach (dynamic d in jArr)
        {
            var tType = new TransportCurrentCategoryType();
            ParseTransportCurrentCategoryType(tType, d);
            tTypes.Add(tType);
        }
    }

    public void WriteBusModelSpecs()
    {
        var jStr = GetJSONString("GetBusModelSpecs");
        var jArr = JArray.Parse(jStr);
        var specs = new List<Characteristic>();
        foreach (dynamic d in jArr)
        {
            var spec = new Characteristic();
            ParseBusModelSpecs(spec, d);
            specs.Add(spec);
        }
    }
public void writeTransportCurrenctCategoryTypes()
{
var jStr=GetJSONString(“GetBusTypes”);
var jArr=JArray.Parse(jStr);
var tTypes=新列表();
foreach(jArr中的动态d)
{
var tType=新的TransportCurrentCategoryType();
ParseTransportCurrentCategoryType(tType,d);
tTypes.Add(tType);
}
}
public void WriteBusModelSpecs()
{
var jStr=GetJSONString(“GetBusModelSpecs”);
var jArr=JArray.Parse(jStr);
var specs=新列表();
foreach(jArr中的动态d)
{
var spec=新特性();
规范(规范,d);
规范。添加(规范);
}
}
我尝试将委托与泛型一起使用,但不起作用

    public delegate void ParseParameters<T>(T objectClass,dynamic a);
    private static void ParceBusClass(BusClass busClass,dynamic a)
    {
        busClass.Name = a.Name;
        busClass.Transport = new TransportCategory {Id = a.BusModelCategoryId};
    }
公共委托参数(T objectClass,动态a);
专用静态void ParceBusClass(BusClass BusClass,动态a)
{
busClass.Name=a.Name;
busClass.Transport=new TransportCategory{Id=a.BusModelCategoryId};
}
那么我叫它:

     GetCollectionFromJSON<BusClass>("", ParceBusClass);
       private static List<T> GetCollectionFromJSON<T>(string functionName,                       ParseParameters<T> parseFunk){
    /****/
     parseFunk<T>(busClass, a);
    /***/
     }
GetCollectionFromJSON(“,ParceBusClass”);
私有静态列表GetCollectionFromJSON(字符串函数名,ParseParameters parseFunk){
/****/
parseFunk(巴士级,a);
/***/
}

它会出错,

通常,您可以使用以下选项:

public List<T> Write<T>(string name, Func<T> factory, Action<T, dynamic> parser)
{
    var jStr = GetJSONString(name);
    var jArr = JArray.Parse(jStr);
    var result = new List<T>();
    foreach (dynamic d in jArr)
    {
        var item = factory();
        parser(item, d);
        result.Add(item);
    }
    return result;
}
公共列表写入(字符串名称、函数工厂、操作解析器)
{
var jStr=GetJSONString(名称);
var jArr=JArray.Parse(jStr);
var result=新列表();
foreach(jArr中的动态d)
{
var item=factory();
语法分析器(项目d);
结果.添加(项目);
}
返回结果;
}
你可以这样称呼它:

Write<Characteristic>(
    "GetBusModelSpecs", () => new Characteristic(), ParseBusModelSpecs);
Write<TransportCurrentCategoryType>(
    "GetBusTypes", () => new TransportCurrentCategoryType(),
    ParseTransportCurrentCategoryType);
写入(
“GetBusModelSpecs”,()=>新特性(),ParseBusModelSpecs);
写(
“GetBustTypes”,()=>新的TransportCurrentCategoryType(),
ParseTransportCurrentCategoryType);
如果大多数或所有类都有默认构造函数,则可以通过提供重载来缩短此过程:

public List<T> Write<T>(string name, Action<T, dynamic> parser)
    where T : new()
{
    return Write<T>(name, () => new T(), parser);
}
公共列表写入(字符串名称、操作解析器)
其中T:new()
{
返回Write(name,()=>newt(),解析器);
}
现在你可以这样称呼它:

Write<Characteristic>("GetBusModelSpecs", ParseBusModelSpecs);
Write<TransportCurrentCategoryType>(
    "GetBusTypes" ,ParseTransportCurrentCategoryType);
Write(“GetBusModelSpecs”,ParseBusModelSpecs);
写(
“GetBusTypes”,ParseTransportCurrentCategoryType);

这个答案没有考虑到可能存在更好的使用JSON库的方法。有关示例,请参见I4V的注释。

使用创建通用抽象父类
Writer
解析json:

public abstract class Writer<T>        
{
    private readonly string _url;

    public Writer(string url)
    {
        _url = url;            
    }

    public void Write()
    {
        var jStr = GetJSONString(_url);
        var jArr = JArray.Parse(jStr);
        var tTypes = new List<T>();

        foreach (dynamic d in jArr)            
            tTypes.Add(Parse(d));           
        // other logic here            
    }

    protected abstract T Parse(object d); // implemented by concrete writers

    private string GetJSONString(string url)
    {
        // getting json string here
    }
}

没有重复的代码。易于阅读和理解。

请尝试
T obj=JsonConvert.DeserializeObject(jStr)
@I4V我不能这样做,因为我有这样的属性:busClass.Transport=new TransportCategory{Id=a.BusModelCategoryId};谢谢,但是var item=factory();不起作用它写道“无法解析符号工厂”@Abbath:确保你完全复制了代码,并且没有任何打字错误。如果你认为你复制的东西都是正确的,请上传一个截图到某个地方。对不起,错误是主要的,所有的作品都是正确的谢谢:)这是我需要的
public class TransportCurrectCategoryWriter : Writer<TransportCurrectCategory>
{
    public TransportCurrectCategoryWriter()
        : base("GetBusTypes")
    {
    }

    protected override TransportCurrectCategory Parse(object d)
    {
        // parse TransportCurrectCategory and return parsed instance
    }
}
var writer = new TransportCurrectCategoryWriter();
writer.Write();