对于.net 3.5只是咬紧牙关它是最干净的外观解决方案。
public struct Wave{
public X time;
public Y enable;
}
public static Wave GetWaveAnimation()
{
try
{
return (from element in configurations.Elements("Animation")
where element.Attribute("NAME").Value == "Wave"
select new Wave
{
time = element.Attribute("TIMING").Value,
enable = element.Attribute("ENABLED").Value
}).FirstOrDefault();
}
catch { return null; }
}
对于.NET 4.0您可以使用动态关键字(但因为匿名类型是内部的,你不能从你的程序集或朋友组件的外部调用此方法。)
public static dynamic GetWaveAnimation()
{
try
{
return (from element in configurations.Elements("Animation")
where element.Attribute("NAME").Value == "Wave"
select new
{
time = element.Attribute("TIMING").Value,
enable = element.Attribute("ENABLED").Value
}).FirstOrDefault();
}
catch { return null; }
}
OR你的元组选
public static Tuple<X,Y> GetWaveAnimation()
{
try
{
return (from element in configurations.Elements("Animation")
where element.Attribute("NAME").Value == "Wave"
select Tuple.Create(
element.Attribute("TIMING").Value,
element.Attribute("ENABLED").Value
)
}).FirstOrDefault();
}
catch { return null; }
}
什么是错误信息?你知道你不能使用'var'让编译器为你推断返回类型,但是返回'object'应该编译。 – 2011-03-10 10:19:59
Noooooo ......删除那个catch块。你为什么认为你需要它? – 2011-03-10 10:20:16
哦,你是对的,一开始我还没有“FirstOrDefault()”,我不确定如果函数抛出一个异常,如果它找不到XElement – jwillmer 2011-03-10 10:35:09