对于.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,您可以使用 dynamic 关键字(但您不能从您的程序集或朋友程序集外部调用此方法,因为匿名类型是内部的。)
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; }
}
或者你有元组选项
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; }
}