如何编写扩展方法以返回使用的类型? - c#

我有一个扩展方法,允许用户关闭延迟加载。

public static IBaseEntityService<TEntity, TPrimaryKey> EnableLazyLoading<TEntity, TPrimaryKey>(
        this IBaseEntityService<TEntity, TPrimaryKey> baseService,
        bool enabled)  
        where TEntity :Entity<TPrimaryKey> where TPrimaryKey : struct
    {
        baseService.UnitOfWork.EnableLazyLoading(enabled);
        baseService.UnitOfWork.EnableProxyCreation(enabled);
        var type = baseService.GetType().;
        return (type)baseService;
    }

这是基本接口的示例:

 public interface IBaseEntityService<TEntity,TPrimaryKey> : 
    IBaseService<TEntity, TPrimaryKey> 
    where TEntity: Entity<TPrimaryKey> where TPrimaryKey : struct
{

    TEntity FindByTenantId(Guid tenantId);
   IBaseEntitySpecification<TEntity,TPrimaryKey> Specification { get; } 
}

假设我有实现IBaseEntityService<TEntity,TPrimaryKey>接口的此服务:

public IUserService: IBaseEntityService<User,int>
{
      User FindByUserName(string username);
}

这是我要实现的目标:

var user = _userService.EnableLazyLoading(false).FindByUserName("someUsername");

如您在示例中在EnableLazyLoading(false)之后看到的,我可以进入FindByUserName方法。当前,由于它仅返回IBaseEntityService,所以我没有该选项。

我想确保扩展方法知道返回IUserService,因为它实现了IBaseEntityService。我知道它必须在某个时候进行转换,并且我想避免必须为EnableLazyLoading编写相同的IUserService方法的特定实现。

我想到了类似的方法,但似乎可以不必隐式调用Cast方法就可以执行某些操作:

public static TEntityService Cast<TEntity, TPrimaryKey, TEntityService>(
            this IBaseEntityService<TEntity, TPrimaryKey> baseEntityService)
            where TEntity : Entity<TPrimaryKey>
            where TPrimaryKey : struct
            where TEntityService : IBaseEntityService<TEntity, TPrimaryKey>
        {
            return (TEntityService) baseEntityService;
        }

所以它可能像这样工作:

var user = _userService.EnableLazyLoading(false).Cast<IUserService>().FindByUserName("someUsername");

参考方案

您可以使EnableLazyLoading的接收者成为受约束为IBaseService的通用类型。有点罗,,但应该做到这一点。

public static TService EnableLazyLoading<TService, TEntity, TKey>(this TService service)
    where TService : IBaseService<TEntity, TKey>
    where TEntity : Entity<TKey>
    where TKey : struct
{
    // do stuff
    return service;
}

这样,您将实际的服务类型作为返回类型。我有一段时间没有做C#了,所以我可能忘记了一些重要的东西,但是IIRC可以正常工作。

将谓词<T>转换为Func <T,bool> - c#

我有一个包含成员Predicate的类,希望在Linq表达式中使用该类:using System.Linq; class MyClass { public bool DoAllHaveSomeProperty() { return m_instrumentList.All(m_filterExpression); } private IEnumerable&…

将列表<>写入数据库asp.net - c#

我被困写数据库不要把我丢错写命令不起作用控制器:[HttpPost] public async Task<ActionResult>PartialTabelaEcp(string userDate) { var numerMiesiaca = 1; var numerRoku = 1; var dbExists = _ecpContext.Kar…

.NET XPathNavigator找不到XPath查询中指定的元素(但是XPath查询在XMLSpy中有效) - c#

我有以下C#代码段,用于使用XPath在正在输入的XML文件中查找错误:string xml; // the XML is passed as a parameter as the string below using (Stream messageStream = new MemoryStream(xml)) { IXPathNavigable sourc…

客户端反序列化为数组序列化字典<string,string>数据 - c#

我有一个字典,该字典使用C#中的JavaScriptSerializer进行了序列化。在客户端,我有:"{"dd049eda-e289-4ca2-8841-4908f94d5b65":"2","ab969ac2-320e-42e1-b759-038eb7f57178":"5�…

Maven的Shade插件产生的罐子之间有什么区别? - java

在我的Maven项目中,我尝试使用maven-shade-plugin在运行mvn package时生成一个超级jar。结果,我的目标目录中出现三个jar:original-hello-world-0.1.0-SNAPSHOT.jar hello-world-0.1.0-SNAPSHOT.jar hello-world-0.1.0-SNAPSHOT-shad…