首页 \ 问答 \ 拦截Ninject拦截扩展中的接口方法(Intercept interface methods in Ninject Interception Extension)

拦截Ninject拦截扩展中的接口方法(Intercept interface methods in Ninject Interception Extension)

我正在玩Ninject拦截扩展。 Ian Davis关于它的博客文章表明,拦截总是基于实际的服务类型,而不是接口。 例如,以下代码将无效,因为IFoo是一个接口:

Kernel.InterceptBefore<IFoo>(f => f.DoSomething(), 
    i => Console.WriteLine("before"));

当然,下一个代码片只有在Foo.DoSomethingvirtual的情况下才有效:

Kernel.InterceptBefore<Foo>(f => f.DoSomething(), 
    i => Console.WriteLine("before"));

在涉及面向方面的编程时,这似乎是一个非常明显的漏洞。 我一直非常认真地对接口进行编程,以便我们可以使用模拟框架来模拟各种服务,但绝大多数实际的方法实现都不是虚拟的。 如果一个模拟框架可以生成一个IFoo ,其方法IFoo我的要求,那么Ninject似乎应该可以。

所以我想我的问题是双重的:

  1. 是否有任何理由Ninject Interception不允许您绑定到接口方法?
  2. 有没有一种简单的方法可以让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 is virtual:

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:

  1. Is there any reason Ninject Interception doesn't allow you to bind to interface methods?
  2. 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
更新时间:2023-08-20 20:08

最满意答案

好的,谢谢你展示你的代码(如果你编辑问题而不是把它放到评论中,下次会更好)。

这将加载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 ...
  • 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 ...
  • 最简单的方法是通过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 ...
  • 执行以下步骤: 在查询中添加一个标准: 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 ...

相关文章

更多

最新问答

更多
  • 您如何使用git diff文件,并将其应用于同一存储库的副本的本地分支?(How do you take a git diff file, and apply it to a local branch that is a copy of the same repository?)
  • 将长浮点值剪切为2个小数点并复制到字符数组(Cut Long Float Value to 2 decimal points and copy to Character Array)
  • OctoberCMS侧边栏不呈现(OctoberCMS Sidebar not rendering)
  • 页面加载后对象是否有资格进行垃圾回收?(Are objects eligible for garbage collection after the page loads?)
  • codeigniter中的语言不能按预期工作(language in codeigniter doesn' t work as expected)
  • 在计算机拍照在哪里进入
  • 使用cin.get()从c ++中的输入流中丢弃不需要的字符(Using cin.get() to discard unwanted characters from the input stream in c++)
  • No for循环将在for循环中运行。(No for loop will run inside for loop. Testing for primes)
  • 单页应用程序:页面重新加载(Single Page Application: page reload)
  • 在循环中选择具有相似模式的列名称(Selecting Column Name With Similar Pattern in a Loop)
  • System.StackOverflow错误(System.StackOverflow error)
  • KnockoutJS未在嵌套模板上应用beforeRemove和afterAdd(KnockoutJS not applying beforeRemove and afterAdd on nested templates)
  • 散列包括方法和/或嵌套属性(Hash include methods and/or nested attributes)
  • android - 如何避免使用Samsung RFS文件系统延迟/冻结?(android - how to avoid lag/freezes with Samsung RFS filesystem?)
  • TensorFlow:基于索引列表创建新张量(TensorFlow: Create a new tensor based on list of indices)
  • 企业安全培训的各项内容
  • 错误:RPC失败;(error: RPC failed; curl transfer closed with outstanding read data remaining)
  • C#类名中允许哪些字符?(What characters are allowed in C# class name?)
  • NumPy:将int64值存储在np.array中并使用dtype float64并将其转换回整数是否安全?(NumPy: Is it safe to store an int64 value in an np.array with dtype float64 and later convert it back to integer?)
  • 注销后如何隐藏导航portlet?(How to hide navigation portlet after logout?)
  • 将多个行和可变行移动到列(moving multiple and variable rows to columns)
  • 提交表单时忽略基础href,而不使用Javascript(ignore base href when submitting form, without using Javascript)
  • 对setOnInfoWindowClickListener的意图(Intent on setOnInfoWindowClickListener)
  • Angular $资源不会改变方法(Angular $resource doesn't change method)
  • 在Angular 5中不是一个函数(is not a function in Angular 5)
  • 如何配置Composite C1以将.m和桌面作为同一站点提供服务(How to configure Composite C1 to serve .m and desktop as the same site)
  • 不适用:悬停在悬停时:在元素之前[复制](Don't apply :hover when hovering on :before element [duplicate])
  • 常见的python rpc和cli接口(Common python rpc and cli interface)
  • Mysql DB单个字段匹配多个其他字段(Mysql DB single field matching to multiple other fields)
  • 产品页面上的Magento Up出售对齐问题(Magento Up sell alignment issue on the products page)