如何访问其范围之外的匿名对象的属性? [英] How do I access an anonymous object's properties outside its scope?
本文介绍了如何访问其范围之外的匿名对象的属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
范围可能不是正确的词.使用以下代码,我无法访问collections元素的对象的属性.是否有更好的返回数据类型或访问这些属性的方法?
Scope might not be the right word. With the following code I can't get access to the collections element's object's properties. Is there a better return data type or a way to access those properties?
protected object Modules()
{
DirectoryInfo di = new DirectoryInfo(Server.MapPath("~/"));
var folders = from x in di.GetDirectories()
where
!(new string[] {
"app_data","bin","obj","scripts","styles","properties"
}).Contains<string>(x.Name.ToLower())
select new
{
Name = x.Name,
Path = x.FullName
};
return folders
}
推荐答案
有两种可行的解决方案:
There are two feasible solutions:
- 为此创建一个真实的类.那是首选的解决方案.
- 使您的方法返回
dynamic
而不是object
.这样做的问题是,您会丢失使用此方法的结果的代码的编译时检查.
- Create a real class for it. That's the prefered solution.
- Make your method return
dynamic
instead ofobject
. The problem with this is that you lose compile time checking of the code that uses the result of this method.
这篇关于如何访问其范围之外的匿名对象的属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文