lambda表达式作为属性(lambda Expression as a property)
我有一个工作设置,它不是强类型的,而是依赖于反射。
说,我有一堂课
class Person{ public string FirstName {get ; set;} public string LastName {get; set;} public int Age {get; set;} ... // some more public properties }
和
class CellInfo { public string Title {get; set;} public string FormatString {get; set;} }
我有这样的字典
Dictionary<string, CellInfo> fields = new Dictionary<string, CellInfo>(); fields.Add("FirstName", new CellInfo {Title = "First Name", FormatString = "Foo"}); fields.Add("LastName", new CellInfo {Title = "Last Name", FormatString = "Bar"});
这是一个简单的字典,其中包含属性名称和一些关于它们的信息 我将字典传递给处理Person实例的另一个模块,而我也是如此
Dictionary<string, CellInfo> fields = SomeMethodToGetDictionary(); foreach(Person p in someCollection) { foreach(var field in fields) { object cellValue = type(Person).GetProperty(field.Key).GetValue(p, null); // use cellValue and info on field from field.Value somewhere. ... } }
这种传递字段名称和使用反射的字符串的方法可行,但我想知道是否有强制类型的方法。
我想到的是有一个存储linq表达式的属性,就像这样
fields.Add("FirstName", new CellInfo { Title = "First Name", FormatString = "Foo", EvalExpression = p => p.FirstName });
并在使用过程中,以某种方式在人物对象上使用
EvalExpression
并获取属性值。 我不知道从哪里开始,或者有什么样的语法可以让这样的属性可以评估。 我很新的功能代表和表达式树,我甚至不知道正确的关键字来搜索。 希望我的描述很明确; 如果没有,请告诉我,我会在必要时提供详情。 任何援助将不胜感激。I have a working setup which is not strongly typed and relies on reflection.
I have a class, say
class Person{ public string FirstName {get ; set;} public string LastName {get; set;} public int Age {get; set;} ... // some more public properties }
and
class CellInfo { public string Title {get; set;} public string FormatString {get; set;} }
and I have a dictionary like this
Dictionary<string, CellInfo> fields = new Dictionary<string, CellInfo>(); fields.Add("FirstName", new CellInfo {Title = "First Name", FormatString = "Foo"}); fields.Add("LastName", new CellInfo {Title = "Last Name", FormatString = "Bar"});
It's a simple dictionary with property Names and some info about them. I pass the dictionary to another module that processes Person instances and I do
Dictionary<string, CellInfo> fields = SomeMethodToGetDictionary(); foreach(Person p in someCollection) { foreach(var field in fields) { object cellValue = type(Person).GetProperty(field.Key).GetValue(p, null); // use cellValue and info on field from field.Value somewhere. ... } }
This method of passing the string for field name and using reflection works, but I was wondering if there is a strongly-typed method of doing this.
What I had in mind was having a property that stored a linq expression, something like this
fields.Add("FirstName", new CellInfo { Title = "First Name", FormatString = "Foo", EvalExpression = p => p.FirstName });
and during usage, somehow use the
EvalExpression
on a person object and get the property value. I have no clue where to begin or what the syntax would be like to have a property like this that's evaluateable. I'm new to function delegates and expression trees that I don't even know the right keywords to search for. Hope my description is clear; if not, let me know and I'll details as necessary. Any assistance would much appreciated.
原文:https://stackoverflow.com/questions/7527536
最满意答案
你是否试图在测试浏览器中显示额外的测试特性? 如果是这样,则可以使用Visual Studio 2012 Update 1中添加的“按特性分组”(详情请参阅http://blogs.msdn.com/b/somasegar/archive/2012/11/26/visual-studio -2012-update-1-now-available.aspx ,从http://www.microsoft.com/en-us/download/details.aspx?id=35774下载)。
总之,你可以用类似的东西装饰你的测试
[TestMethod] [TestCategory("SpecialTestType")] [TestProperty("XXX","YYY")] public void TestMethod1() { }
一旦再次发现此测试,请选择“按特性分组”(TextExplorer工具窗口中的工具栏)根据您的特征对测试进行分组(例如,SpecialTestType,XXX)。
Console.WriteLine did the job... The user can click on 'output' and sees the output...
相关问答
更多-
用额外的信息扩展Visual Studio 2012测试资源管理器(Extend Visual Studio 2012 Test Explorer with extra information)[2023-11-15]
你是否试图在测试浏览器中显示额外的测试特性? 如果是这样,则可以使用Visual Studio 2012 Update 1中添加的“按特性分组”(详情请参阅http://blogs.msdn.com/b/somasegar/archive/2012/11/26/visual-studio -2012-update-1-now-available.aspx ,从http://www.microsoft.com/en-us/download/details.aspx?id=35774下载)。 总之,你可以用类似 ... -
在Visual Studio 2012中打开TFS源代码管理资源管理器的热键?(Hotkey to open TFS Source Control Explorer in Visual Studio 2012?)[2023-06-14]
尝试以下步骤: 转到工具菜单选项 从环境中选择键盘 从显示命令中选择View.TfsSourceControlExplorer 包含: 。 您可以在文本框中键入View.TFS来过滤列表。 在按shorcut keys =>点击分配输入您的热键 希望有帮助! Try with below step: Go to Options of Tools menu Select the Keyboard from Environment Select View.TfsSourceControlExplorer fro ... -
解决方案浏览器的顶部是一个切换按钮“显示所有文件”,这使得它将所有文件包含在显示中,即使它们不包含在项目中。 这些图标将会变灰。 然后只需使用上下文菜单将它们添加到项目中。 At the top of the solution explorer is a toggle button "Show All Files" this makes it include all files in the display even if they aren't included in the project. The i ...
-
请尝试使用HResult。 Assert.AreEqual(val, e.HResult) 不应该根据语言改变。 HResult是分配给特定异常的编码数值,因此使用HResult可以知道抛出了哪个异常。 编辑: 或者,如果您知道您期望的异常,您可以编写自己的异常 - “expecter”。 protected static void ThrowsException
(Action action) where T : Exception { var thrown = false; ... -
卸载VS2012的所有组件并重新安装它们就可以了。 当其他一切都很好的时候,不确定为什么这么小的功能会丢失。 Uninstalling ALL components of VS2012 and re-installing them did the trick. Unsure why such a small feature was missing when everything else worked great.
-
你的chutzpah.json错了。 以下更新的应该可以正常工作。 由于你有一个编译配置集,你不需要引用.js文件,而是应该引用.ts文件,让Chutzpah找到匹配的.js文件。 { "Compile": { "Mode": "External", "Extensions": [".ts"], "ExtensionsWithNoOutput": [".d.ts"] }, "References": [ { "Path": "../Scripts/jquery-1.11.0.js" }, { "Path": ...
-
为什么Visual Studio的Test Explorer没有找到这个测试?(Why is Visual Studio's Test Explorer not finding this test?)[2023-12-10]
问题与“(除项目名称之外的所有设置都相同)有关。” 名为MyCo.Storage.Testing的项目有一个名为:MyCo.Storage的程序集,然后与引用的项目冲突,后者生成一个具有相同名称的程序集。 将程序集的名称更改为MyCo.Storage.Testing解决了问题。 The problem was related to "(all setting except the project name are the same)." The project named MyCo.Storage.Test ... -
要在Visual Studio中显示关联文件的图标,需要将它们重新命名为windows系统中的文件扩展名(请参阅创建注册表项以将文件扩展名与C ++中的应用程序关联 )。 我们遇到的问题非常具体。 因为我们使用的扩展名安装在相应用户的userprofile中,并且图标文件也在该目录中,所以我们使用以下路径注册它: %USERPROFILE%\应用程序数据\本地\微软\ VisualStudio的\ 14.0 \扩展...... 现在的问题是, Visual Studio 2015大部分都没有在相应的用户帐户 ...
-
我用最新的更新Visual Studio 2012 - Update 5 https://www.microsoft.com/en-us/download/details.aspx?id=48708 I Updates the Visual Studio 2012 with latest -- Update 5 https://www.microsoft.com/en-us/download/details.aspx?id=48708
-
在您的解决方案构建配置中,测试项目是否已设置为构建? It appears that this is a bug in Visual Studio 2012. The workaround I'm currently using is to run the tests from Resharper's Test Interface. Hopefully there will be a fix for the problem soon.