如何声明匿名类型(C#)的字段

如何声明匿名类型(C#)的字段,c#,ienumerable,C#,Ienumerable,在下面的代码中,如何将myLine声明为公共(全局)变量?问题是我不能使用关键字“var” 公共静态IEnumerable读线(StreamReader) { 而(!reader.EndOfStream) { 产生返回reader.ReadLine(); } } 专用无效筛选器1(字符串文件名) { 使用(var writer=File.CreateText(Application.StartupPath+“\\temp\\test.txt”)) { 使用(var reader=File.Ope

在下面的代码中,如何将myLine声明为公共(全局)变量?问题是我不能使用关键字“var”

公共静态IEnumerable读线(StreamReader)
{
而(!reader.EndOfStream)
{
产生返回reader.ReadLine();
}
}
专用无效筛选器1(字符串文件名)
{
使用(var writer=File.CreateText(Application.StartupPath+“\\temp\\test.txt”))
{
使用(var reader=File.OpenText(文件名))
{
int[]id={14652、14653、14654、14655、14656、14657、14658、14659、14660};
var myLine=从读取行中的行(读卡器)
其中line.Length>1
让id=int.Parse(line.Split('\t')[1])
其中Ids.Contains(id)
让m=Regex.Match(行@“^\d+\t(\d+\t.+?\t(项\\[^\t]+\.ddj)”)
其中m.Success==true
选择新{Text=line,ItemId=id,Path=m.Groups[2].Value};
foreach(myLine中的变量id)
{
writer.WriteLine(“项目Id=“+Id.ItemId”);
writer.WriteLine(“Path=“+id.Path”);
writer.WriteLine(“\n”);
}
}
}
}

我想这样做,因为我必须找到一种方法来获得对ienumerable的访问权,以备以后使用。

问题在于它使用的是匿名类型,您不能在字段声明中使用

解决方法是编写具有相同成员的命名类型,并在查询中使用该类型。如果希望它具有与匿名类型相同的行为,它应该:

  • 获取构造函数中的所有值
  • 不可变,公开只读属性
  • 覆盖
    等于
    GetHashCode
    ToString
  • 封存
您可以使用Reflector来反编译代码,但最终会得到一个您并不真正需要的泛型类型

这个类看起来像:

public sealed class Foo
{
    private readonly string text;
    private readonly int itemId;
    private readonly string path;

    public Foo(string text, int itemId, string path)
    {
        this.text = text;
        this.itemId = itemId;
        this.path = path;
    }

    public string Text
    {
        get { return text; }
    }

    public int ItemId
    {
        get { return itemId; }
    }

    public string Path
    {
        get { return path; }
    }

    public override bool Equals(object other)
    {
        Foo otherFoo = other as Foo;
        if (otherFoo == null)
        {
            return false;
        }
        return EqualityComparer<string>.Default.Equals(text, otherFoo.text) &&
        return EqualityComparer<int>.Default.Equals(itemId, otherFoo.itemId) &&
        return EqualityComparer<string>.Default.Equals(path, otherFoo.path);
    }

    public override string ToString()
    {
        return string.Format("{{ Text={0}, ItemId={1}, Path={2} }}",
                             text, itemId, path);
    }

    public override int GetHashCode()
    {
        int hash = 17;
        hash = hash * 23 + EqualityComparer<string>.Default.GetHashCode(text);
        hash = hash * 23 + EqualityComparer<int>.Default.GetHashCode(itemId);
        hash = hash * 23 + EqualityComparer<string>.Default.GetHashCode(path);
        return hash;
    }
}

不要在新键盘上使用匿名类,而是使用文本、ItemId等显式定义一个类。这样类型将是IQueryable。使用它而不是var关键字。

基本上我为每个ItemId创建了一个Foo类的实例,对吗?如果我能再问你一个问题,你为什么要把它们宣布为私人的?几个月前,您教我将它们声明为公共字符串文本{get;private set};这是用公共getter和私有setter声明属性。这里我们有属性的公共getter,没有setter,还有私有字段。如果可能的话,我更喜欢完全只读的类。哦,我忘了最重要的一点。使用你的代码,IEnumerable的类型是什么?IEnumerable?是的,它将是IEnumerable。
public sealed class Foo
{
    private readonly string text;
    private readonly int itemId;
    private readonly string path;

    public Foo(string text, int itemId, string path)
    {
        this.text = text;
        this.itemId = itemId;
        this.path = path;
    }

    public string Text
    {
        get { return text; }
    }

    public int ItemId
    {
        get { return itemId; }
    }

    public string Path
    {
        get { return path; }
    }

    public override bool Equals(object other)
    {
        Foo otherFoo = other as Foo;
        if (otherFoo == null)
        {
            return false;
        }
        return EqualityComparer<string>.Default.Equals(text, otherFoo.text) &&
        return EqualityComparer<int>.Default.Equals(itemId, otherFoo.itemId) &&
        return EqualityComparer<string>.Default.Equals(path, otherFoo.path);
    }

    public override string ToString()
    {
        return string.Format("{{ Text={0}, ItemId={1}, Path={2} }}",
                             text, itemId, path);
    }

    public override int GetHashCode()
    {
        int hash = 17;
        hash = hash * 23 + EqualityComparer<string>.Default.GetHashCode(text);
        hash = hash * 23 + EqualityComparer<int>.Default.GetHashCode(itemId);
        hash = hash * 23 + EqualityComparer<string>.Default.GetHashCode(path);
        return hash;
    }
}
select new Foo(line, id, m.Groups[2].Value)