在引用的程序集扩展方法? [英] Extension methods in referenced assemblies?

查看:111
本文介绍了在引用的程序集扩展方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我尝试调用它的定义是这样我的扩展方法:

 模块LinqExtensions
&其中; System.Runtime.CompilerServices.Extension()> _
公共功能ToSortableBindingList(TSource中)(来源BYVAL为IEnumerable(TSource中))作为IBindingList的
    如果(源是没有什么)然后
        抛出新ArgumentNullException(源)
    结束如果
    返回新SortableBindingList(TSource中)(新的列表(TSource中)(来源))
端功能
前端模块
 

致电

 昏暗SSS作为字符串()
   sss.AsEnumerable.ToSortableBindingList()
 

它给出​​了一个错误ToSortableBindingList不System.Collections.Generic.IEnumerable(串)的一员。

注:上期后,智能感知自动完成!如果我尝试调用context.TestTable.AsEnumerable.ToSortableBindingList(TestTable的是一个纯EF4生成的类),它甚至没有出现与智能感知。我不知道为什么。什么是错的扩展方法声明BYVAL来源为IEnumerable(中TSource)?

的**的 * 的**的 * 的**的 * 的**的 * 的**的 * 的***的 修改 的**的 * 的**的 * **的 * 的**的 * 的**的 * 的**

好吧,澄清所发生的事情,我想提供一些额外的信息。 我可以追查问题如下:

方案:

Assembly1(根命名空间的myapp):

  ...
     &其中; System.Runtime.CompilerServices.Extension()> _
        公共职能ToTest(来源BYVAL作为字符串)作为字符串
            返回 
        端功能
...
 

呼唤作品:

  ...
黯淡了作为字符串
a.ToTest()
...
 

Assembly2: (参考Assembly1含税)

呼叫不起作用:

 进口的myapp
...
黯淡了作为字符串
a.ToTest()
 

解决方案

您的命名空间的myapp不能直接包含的功能ToTest,有定义有一个模块。 尝试

 导入myapp.LinqExtensions
 

和确保它是一个公共模块

If I try to call my extension method which is defined like this:

Module LinqExtensions
<System.Runtime.CompilerServices.Extension()> _
Public Function ToSortableBindingList(Of TSource)(ByVal source As IEnumerable(Of TSource)) As IBindingList
    If (source Is Nothing) Then
        Throw New ArgumentNullException("source")
    End If
    Return New SortableBindingList(Of TSource)(New List(Of TSource)(source))
End Function
End Module

by calling

   Dim sss As String()
   sss.AsEnumerable.ToSortableBindingList()

it gives an error "ToSortableBindingList is not a member of System.Collections.Generic.IEnumerable(Of String)".

Note: Intellisense autocompletes after the last period! If I try to call context.TestTable.AsEnumerable.ToSortableBindingList (TestTable is a pure EF4 generated class) it not even shows up with intellisense. I don't get why. What's wrong with the extension method declaration "ByVal source As IEnumerable(Of TSource)"?

****************** EDIT *****************

Ok, to clarify what's happening I'd like to provide some additional info. I can track down the problem to the following:

Scenario:

Assembly1 (root namespace "myapp"):

...
     <System.Runtime.CompilerServices.Extension()> _
        Public Function ToTest(ByVal source As String) As String
            Return ""
        End Function
...

'Calling works:

...
Dim a as string
a.ToTest()
...

Assembly2: (Reference to Assembly1 included)

'Calling does not work:

imports myapp
...
Dim a as string
a.ToTest()

解决方案

Your namespace "myapp" cannot directly contain the function "ToTest", there is a Module defined there. Try

Imports myapp.LinqExtensions

and make sure it is a Public Module

这篇关于在引用的程序集扩展方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆