拦截Ninject拦截扩展中的接口方法(Intercept interface methods in Ninject Interception Extension)
我正在玩Ninject拦截扩展。 Ian Davis关于它的博客文章表明,拦截总是基于实际的服务类型,而不是接口。 例如,以下代码将无效,因为
IFoo
是一个接口:Kernel.InterceptBefore<IFoo>(f => f.DoSomething(), i => Console.WriteLine("before"));
当然,下一个代码片只有在
Foo.DoSomething
是virtual
的情况下才有效:Kernel.InterceptBefore<Foo>(f => f.DoSomething(), i => Console.WriteLine("before"));
在涉及面向方面的编程时,这似乎是一个非常明显的漏洞。 我一直非常认真地对接口进行编程,以便我们可以使用模拟框架来模拟各种服务,但绝大多数实际的方法实现都不是虚拟的。 如果一个模拟框架可以生成一个
IFoo
,其方法IFoo
我的要求,那么Ninject似乎应该可以。所以我想我的问题是双重的:
- 是否有任何理由Ninject Interception不允许您绑定到接口方法?
- 有没有一种简单的方法可以让Ninject绑定到动态“包装器”类,这些类可以让我对所有接口方法执行某些拦截操作,然后将调用传递给真正的实现?
I'm playing around with the Ninject Interception extension. Ian Davis's blog post about it indicates that interception is always based on the actual service type, rather than the interface. For example, the following code will have no effect because
IFoo
is an interface:Kernel.InterceptBefore<IFoo>(f => f.DoSomething(), i => Console.WriteLine("before"));
And of course, the next code piece will only work if
Foo.DoSomething
isvirtual
:Kernel.InterceptBefore<Foo>(f => f.DoSomething(), i => Console.WriteLine("before"));
This seems like a pretty glaring hole when it comes to Aspect-Oriented programming. I've been pretty conscientious about programming to interfaces so that we could use mocking frameworks to mock our various services, but the vast majority of my actual method implementations are not virtual. If a mocking framework can produce an
IFoo
with a method that does what I ask for, it seems like Ninject ought to be able to.So I guess my question is two-fold:
- Is there any reason Ninject Interception doesn't allow you to bind to interface methods?
- Is there an easy way to make Ninject bind to dynamic "wrapper" classes that can let me perform certain interception actions on all the interface methods, and then pass the call through to the real implementation?
原文:https://stackoverflow.com/questions/4950965
最满意答案
好的,谢谢你展示你的代码(如果你编辑问题而不是把它放到评论中,下次会更好)。
这将加载CSV文件,并且对于每个记录,它将用任何内容替换不需要的文本,并输出修改的记录,并将输出到更新的文件。
import-csv C:\temp\Assets.csv|%{ $_.UserName = $_.UserName -replace ".*;#" $_ }|export-csv c:\temp\assets2.csv -notype
Ok, thank you for showing your code (next time it's better if you edit the question rather than putting it into a comment).
This will load the CSV file, and for each record it will replace the undesirable text with nothing, and output the modified record, and will output to an updated file.
import-csv C:\temp\Assets.csv|%{ $_.UserName = $_.UserName -replace ".*;#" $_ }|export-csv c:\temp\assets2.csv -notype
相关问答
更多-
听起来像PowerShell脚本的工作: 读取CSV文件非常简单( Import-Csv cmdlet) 可以使用Windows资源管理器中的WebDav集成访问文档库 文件操作很简单( Copy-Item cmdlet) Sounds like a job for a PowerShell script: reading a CSV file is very simple (Import-Csv cmdlet) document library can be accessed using WebDav i ...
-
如何在AngularJS中将List / Collection数据表导出到csv(How to export List/Collection of data tables to csv in AngularJS)[2021-07-17]
CSV不支持多张表概念。 您可以为每个表格创建多个CSV文件。 这是一个相同的指令。 http://ngmodules.org/modules/ng-csv 如果你想在同一张表中导出,你可能想在服务器端“导出为excel”。 在Java的情况下,Apache POI是一个很好的选择。 https://poi.apache.org/spreadsheet/examples.html CSV does not support multiple sheets concept. You can create mul ... -
以HTML格式导出Sharepoint列表的最简单方法是什么?(What is the easiest approach to exporting a Sharepoint list in HTML format?)[2022-06-19]
最简单的方法是通过JavaScript获取SharePoint列表内容并显示为HTML格式。 CodePlex有一个很棒的库,名为http://spservices.codeplex.com/ 。 另请参阅http://www.codeproject.com/Articles/544538/JQuery-with-SharePoint-2010 The easiest approach would be getting the SharePoint list content through JavaScri ... -
将输出保存到DataTable 。 然后尝试这个 CSVWriter writer = new CSVWriter(yourDataTable); byte[] datatoexport = writer.ExportToBytes(); string fileName = string.Format("Export-{0}.csv", DateTime.Now.ToString("yyyyMMddhhmm")); return File(datatoexport, "text/csv", fileName ...
-
正如Joe在评论中所建议的那样,除非您出于其他原因需要csv文件,否则最好为此创建一个SAS数据库。 另一种方法是使用proc import它几乎与使用proc export方式相同: proc import datafile="&Filepath./CA_ISO_policyBYpolicy_&thestate..csv" out=Work.CA_ISO_policyBYpolicy_&thestate. dbms=dlm replace; delimiter=","; getnames ...
-
批量导出为CSV(Batch Exporting to CSV)[2022-10-04]
执行以下步骤: 在查询中添加一个标准: WHERE NOT isProcessed ,因此接下来执行导出时,只会处理未处理的记录。 完成导出后,将此命令发送到数据库: "UPDATE " + tableName + " SET isProcessed=true WHERE" 。 这样,所有记录现在都标记为已处理。 包装TransactionScope arround export-mechanisme包括。 更新。 ... -
你需要DictWriter 演示: import csv my_list= [ {'user': 1000, 'account1': 100, 'account2': 200, 'account3': 100}, {'user': 1001, 'account1': 110, 'account2': 100, 'account3': 250}, {'user': 1002, 'account1': 220, 'account2': 200, 'account3': 100}, ] with open(fi ...
-
你的问题: 我建议您只使用join方法获取您要查找的单个字符串。 你可以尝试: with open('test.csv', 'w') as a_file: for result in C: result = ' '.join(result) a_file.write(result + '\n') 基本上,调用字符串' ' (单个空格)的join方法将获取其参数中的每个项目(来自排列的单个结果),并将它们一起添加到单个字符串中,该字符串由第一个字符串(单个字符串)分隔 ...
-
Powershell用于与SharePoint交互的服务器对象模型与Microsoft.SharePoint.Powershell管理单元只能与在同一服务器上运行的SharePoint环境进行交互。 也就是说,需要与SharePoint场交互的任何此类Powershell脚本都需要从该场的Web前端服务器执行。 如果您从不属于该SharePoint场的服务器运行Powershell,但仍需要访问该服务器场中的列表,请考虑使用SharePoint的客户端对象模型。 由于可以通过.NET代码访问客户端对象模型, ...
-
好的,谢谢你展示你的代码(如果你编辑问题而不是把它放到评论中,下次会更好)。 这将加载CSV文件,并且对于每个记录,它将用任何内容替换不需要的文本,并输出修改的记录,并将输出到更新的文件。 import-csv C:\temp\Assets.csv|%{ $_.UserName = $_.UserName -replace ".*;#" $_ }|export-csv c:\temp\assets2.csv -notype Ok, thank you for showing your co ...