我如何申报场与匿名类型(C#) [英] How do I declare a Field with Anonymous Type (C#)
问题描述
在下面的代码,我怎么声明MYLINE作为公共(全局)变量?但问题是,我不能使用关键字VAR
In the code below,how do I declare myLine as a public(global) variable? The problem is that I can't use the keyword "var".
public static IEnumerable<string> ReadLines(StreamReader reader)
{
while (!reader.EndOfStream)
{
yield return reader.ReadLine();
}
}
private void Filter1(string filename)
{
using(var writer = File.CreateText(Application.StartupPath + "\\temp\\test.txt"))
{
using (var reader = File.OpenText(filename))
{
int[] Ids = { 14652, 14653, 14654, 14655, 14656, 14657, 14658, 14659, 14660 };
var myLine = from line in ReadLines(reader)
where line.Length > 1
let id = int.Parse(line.Split('\t')[1])
where Ids.Contains(id)
let m = Regex.Match(line, @"^\d+\t(\d+)\t.+?\t(item\\[^\t]+\.ddj)")
where m.Success == true
select new { Text = line, ItemId = id, Path = m.Groups[2].Value };
foreach (var id in myLine)
{
writer.WriteLine("Item Id = " + id.ItemId);
writer.WriteLine("Path = " + id.Path);
writer.WriteLine("\n");
}
}
}
}
我想这样做,因为我必须找到一种方式来获得访问该IEnumerable的供以后使用。
I want to do it ,because I have to find a way to gain access to that ienumerable for later use.
推荐答案
麻烦的是,它使用一个匿名类型,你不能在一个字段声明中使用。
The trouble is that it's using an anonymous type, which you can't use in a field declaration.
解决方法是编写一个名为类型,具有相同的成员,并使用该类型的查询。如果你希望它有同样的行为作为您的匿名类型,它应该:
The fix is to write a named type with the same members, and use that type in your query. If you want it to have the same behaviour as your anonymous type, it should:
- 取所有的值在构造
- 是一成不变的,露出只读属性
- 覆盖
等于
,的GetHashCode
和的ToString
- 密封
- Take all the values in the constructor
- Be immutable, exposing read-only properties
- Override
Equals
,GetHashCode
andToString
- Be sealed
您的可能的只是用反射反编译代码,但随后你会最终有一个泛型类型,你并不真正需要的。
You could just use Reflector to decompile the code, but then you'd end up with a generic type which you don't really need.
类看起来是这样的:
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;
}
}
您的查询将只是改变在年底为:
Your query would just change at the end to:
select new Foo(line, id, m.Groups[2].Value)
这篇关于我如何申报场与匿名类型(C#)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!