Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/323.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# - Fatal编程技术网

C# 推广函数

C# 推广函数,c#,C#,我试图找出是否有一种方法可以泛化一个函数,该函数采用两个具有相似属性的不相关对象的哈希集。我在下面有一些示例代码: private IList<IDictionary<string, string>> BuildDictionary(HashSet<ClassA> ClassA) { IList<IDictionary<string, string>> data = new List<IDictionary<stri

我试图找出是否有一种方法可以泛化一个函数,该函数采用两个具有相似属性的不相关对象的哈希集。我在下面有一些示例代码:

private IList<IDictionary<string, string>> BuildDictionary(HashSet<ClassA> ClassA)
{
    IList<IDictionary<string, string>> data = new List<IDictionary<string, string>>();
    foreach (var a in ClassA)
    {
        Dictionary<string, string> aDictionary = new Dictionary<string, string>();
        aDictionary.Add(a.Code, a.Code + "," + a.Name);
        data.Add(aDictionary);
    }

    return data;
}

private IList<IDictionary<string, string>> BuildDictionary(HashSet<ClassB> ClassB)
{
    IList<IDictionary<string, string>> data = new List<IDictionary<string, string>>();
    foreach (var b in ClassB)
    {
        Dictionary<string, string> bDictionary = new Dictionary<string, string>();
        bDictionary.Add(b.Code, b.Code + "," + b.Name);
        data.Add(bDictionary);
    }

    return data;
}
私有IList构建字典(HashSet ClassA)
{
IList数据=新列表();
foreach(a类中的var a)
{
Dictionary addictionary=新字典();
添加(a.代码,a.代码+”,“+a.名称);
添加数据(词典);
}
返回数据;
}
私有IList构建字典(HashSet类B)
{
IList数据=新列表();
foreach(b类中的变量b)
{
字典bDictionary=新字典();
b.添加(b.代码,b.代码+”,“+b.名称);
添加数据(b字典);
}
返回数据;
}

因此,从代码中可以明显看出,这两个类并不相关,但它们都位于哈希集中,并且包含类似的属性(代码、名称)。我曾尝试使用泛型T,但由于没有创建泛型类T,因此失败了。如果您拥有这两种类型的源代码,您可以实现一个通用接口

private IList<IDictionary<string, string>> BuildDictionary<T>(HashSet<T> someHashSetOfTs) where T : ICommon
{
    IList<IDictionary<string, string>> data = new List<IDictionary<string, string>>();
    foreach (var a in someHashSetOfTs)
    {
        Dictionary<string, string> aDictionary = new Dictionary<string, string>();
        aDictionary.Add(a.Code, a.Code + "," + a.Name);
        data.Add(aDictionary);
    }

    return data;
}

现在将
ICommon
应用于两种类型
ClassA
ClassB

如果您拥有这两种类型的源代码,则可以实现一个通用接口

private IList<IDictionary<string, string>> BuildDictionary<T>(HashSet<T> someHashSetOfTs) where T : ICommon
{
    IList<IDictionary<string, string>> data = new List<IDictionary<string, string>>();
    foreach (var a in someHashSetOfTs)
    {
        Dictionary<string, string> aDictionary = new Dictionary<string, string>();
        aDictionary.Add(a.Code, a.Code + "," + a.Name);
        data.Add(aDictionary);
    }

    return data;
}

现在,将
ICommon
应用于
ClassA
ClassB
这两种类型,如果源类已密封或无法修改为公共接口,则可以对所需的部分使用访问器,就像在大多数LINQ查询中一样

下面是一个示例实现。请注意,
toKey()
toMemberValue()
可以更恰当地命名,但这足以复制您对任何类所做的操作,您可以指定lambda来检索相关属性,并且不依赖于类必须具有相同的属性名,只要lambda是相应地编写的
Main()
显示了在这两种情况下使用此方法的效果

public IList<IDictionary<string, string>> BuildDictionary<T>(HashSet<T> sourceSet, Func<T, string> toKey, Func<T, string> toMemberValue)
{
  IList<IDictionary<string, string>> data = new List<IDictionary<string, string>>();

  foreach (var element in sourceSet)
  {
    Dictionary<string, string> newLookup = new Dictionary<string, string>();
    newLookup.Add(toKey(element), $"{toKey(element)},{toMemberValue(element)}");
    data.Add(newLookup);
  }

  return data;
}

void Main()
{
  HashSet<ClassA> setOfAs = new HashSet<ClassA>(new[] { new ClassA { Code = "foo", Name = "bar" }, new ClassA { Code = "foo2", Name = "bar2" } });
  HashSet<ClassB> setOfBs = new HashSet<ClassB>(new[] { new ClassB { Code = "foo", Name = "bar" }, new ClassB { Code = "foo2", Name = "bar2" } });

  var lookupOfAs = BuildDictionary(setOfAs, x => x.Code, x => x.Name);
  var lookupOfBs = BuildDictionary(setOfBs, x => x.Code, x => x.Name);
}

// Define other methods and classes here
public class ClassA
{
  public string Code { get; set; }
  public string Name { get; set; }
}

public class ClassB
{
  public string Code { get; set; }
  public string Name { get; set; }
}
public IList BuildDictionary(HashSet sourceSet、Func toKey、Func toMemberValue)
{
IList数据=新列表();
foreach(sourceSet中的var元素)
{
字典newLookup=新字典();
Add(toKey(element),$“{toKey(element)},{toMemberValue(element)}”);
data.Add(newLookup);
}
返回数据;
}
void Main()
{
HashSet setOfAs=newhashset(new[]{new ClassA{Code=“foo”,Name=“bar”},new ClassA{Code=“foo2”,Name=“bar2”});
HashSet setOfBs=newhashset(new[]{new ClassB{Code=“foo”,Name=“bar”},new ClassB{Code=“foo2”,Name=“bar2”});
var lookupOfAs=BuildDictionary(setOfAs,x=>x.Code,x=>x.Name);
var lookupOfBs=BuildDictionary(setOfBs,x=>x.Code,x=>x.Name);
}
//在此处定义其他方法和类
甲级公共课
{
公共字符串代码{get;set;}
公共字符串名称{get;set;}
}
公共B类
{
公共字符串代码{get;set;}
公共字符串名称{get;set;}
}

如果源类已密封或无法修改为公共接口,则可以对所需的部分使用访问器,就像在大多数LINQ查询中一样

下面是一个示例实现。请注意,
toKey()
toMemberValue()
可以更恰当地命名,但这足以复制您对任何类所做的操作,您可以指定lambda来检索相关属性,并且不依赖于类必须具有相同的属性名,只要lambda是相应地编写的
Main()
显示了在这两种情况下使用此方法的效果

public IList<IDictionary<string, string>> BuildDictionary<T>(HashSet<T> sourceSet, Func<T, string> toKey, Func<T, string> toMemberValue)
{
  IList<IDictionary<string, string>> data = new List<IDictionary<string, string>>();

  foreach (var element in sourceSet)
  {
    Dictionary<string, string> newLookup = new Dictionary<string, string>();
    newLookup.Add(toKey(element), $"{toKey(element)},{toMemberValue(element)}");
    data.Add(newLookup);
  }

  return data;
}

void Main()
{
  HashSet<ClassA> setOfAs = new HashSet<ClassA>(new[] { new ClassA { Code = "foo", Name = "bar" }, new ClassA { Code = "foo2", Name = "bar2" } });
  HashSet<ClassB> setOfBs = new HashSet<ClassB>(new[] { new ClassB { Code = "foo", Name = "bar" }, new ClassB { Code = "foo2", Name = "bar2" } });

  var lookupOfAs = BuildDictionary(setOfAs, x => x.Code, x => x.Name);
  var lookupOfBs = BuildDictionary(setOfBs, x => x.Code, x => x.Name);
}

// Define other methods and classes here
public class ClassA
{
  public string Code { get; set; }
  public string Name { get; set; }
}

public class ClassB
{
  public string Code { get; set; }
  public string Name { get; set; }
}
public IList BuildDictionary(HashSet sourceSet、Func toKey、Func toMemberValue)
{
IList数据=新列表();
foreach(sourceSet中的var元素)
{
字典newLookup=新字典();
Add(toKey(element),$“{toKey(element)},{toMemberValue(element)}”);
data.Add(newLookup);
}
返回数据;
}
void Main()
{
HashSet setOfAs=newhashset(new[]{new ClassA{Code=“foo”,Name=“bar”},new ClassA{Code=“foo2”,Name=“bar2”});
HashSet setOfBs=newhashset(new[]{new ClassB{Code=“foo”,Name=“bar”},new ClassB{Code=“foo2”,Name=“bar2”});
var lookupOfAs=BuildDictionary(setOfAs,x=>x.Code,x=>x.Name);
var lookupOfBs=BuildDictionary(setOfBs,x=>x.Code,x=>x.Name);
}
//在此处定义其他方法和类
甲级公共课
{
公共字符串代码{get;set;}
公共字符串名称{get;set;}
}
公共B类
{
公共字符串代码{get;set;}
公共字符串名称{get;set;}
}

两个类的接口如何?您是否可以访问这两个类以便实现接口?您可以使用反射来检查类是“typeof”还是“is”,并在T:class时使用T两个类的接口如何?您是否都可以访问这两个类以便实现接口?您可以使用反射来检查类是“typeof”还是“is”,并在T:class时使用T