我在解决如何为扩展方法创建Shim时遇到麻烦。
我的程序集“ MJH.Extensions.dll”中具有扩展方法,如下所示: (显然,这不是REAL方法正在执行的操作。.在此示例中,我已对其进行了简化。)
-- insert your user id here:
declare @uid int = 7505395
-- get all badges of all users
select Name, Date, [Gold/Silver/Else], [Row#] from (
SELECT Name,
Date,
userId,
case when class = 1 then 'Gold'
when class = 2 then 'Silver'
when class = 3 then 'Bronze'
else convert(varchar(10), class)
end as 'Gold/Silver/Else',
ROW_NUMBER() OVER(PARTITION BY name, class ORDER BY date ASC) AS Row#
FROM badges
WHERE 1 = 1
-- you can restrict this further, f.e. for looking only by gold badges
-- and Class = 1 -- gold == 1, silver == 2, bronze == 3
-- -- or for certain named badges
-- and name like 'python%'
) as tmp
where userID = @uid
ORDER by name asc, Date asc
您当然可以轻松地在代码中使用它。.
namespace MJH.Extensions {
public static class ListItemExtensions {
public static string foo(this Microsoft.SharePoint.ListItem item) {
return string.Empty;
}
}
}
我正在尝试将这种方法用于单元测试……但是我一生无法解决如何做到这一点。
我为自己的MJH程序集添加了一个“假”程序集,其中包括“ ListItemExtensions”静态类。
我也尝试为“ Microsoft.SharePoint.Client”程序集添加“ Fakes”程序集。
(及其所有排列)。
using Microsoft.SharePoint.Client;
using MJH.Extensions;
...
ListItem item = new ListItem();
item.foo(); // returns empty string