具有嵌套名称的扩展名 [英] Extensions with nested names
问题描述
我使用的第三方库有一个名为SessionManager的连接对象。 SessionManager允许您创建不同类型的查询,添加或修改请求。我正在编写一些扩展方法来尝试使编码更快,因为我倾向于在使用此组件时为许多项目重复代码。这是我用来查询cusotmers列表的常规代码:
SessionManager manager = new SessionManager();
manager.OpenConnection(filename);
IMsgSetRequest MsgRequest = SessionManager.CreateMsgSetRequest();
ICustomerQuery query = MsgRequest.AppendCustomerQuery();
query.ActiveStatus.SetValue(ENActiveStatus.All);
IResponse response = SessionManager.DoRequests(MsgRequest);
if (response.StatusCode == 0 ){ return (ICustomerRetList)response.Detail; }
else { throw new 异常( 查询客户的未知错误: + response.StatusMessage;}
我不知道是否可以使用扩展,但我希望能够更改我的代码来执行以下操作:
SessionManager manager = new SessionManager();
return manager.Customers.QueryAll();
我的想法是,我可以编写一些标准查询,如
manager.Customers.QueryActive();
和
manager.Vendors.QueryAll( );
我无法弄清楚的难点是SessionManager和查询的实际扩展函数之间的部分。看起来你不能做一个带扩展的嵌套类。
我的尝试:
我试过在静态类中创建一个静态类,但是我得到的错误是扩展方法必须在顶级类中。我尝试了以下代码,但编译器似乎并不理解我在尝试的内容:
public static ICustomerRetList Customer.QueryAll(此SessionManager管理器)
您无法创建嵌套扩展方法,但您的扩展方法可以返回其他方法的类/结构实例。 />
尝试这样的事情:
public static class MyExtensions
{
public static CustomerWrapper Customer(此 SessionManager会话)
{
return new CustomerWrapper(session);
}
}
public struct CustomerWrapper
{
public CustomerWrapper(SessionManager会话)
{
Session = session;
}
public SessionManager Session { get ; }
public ICustomerRetList QueryAll()
{
...
}
}
您只需在客户
电话后添加括号:
< pre lang =C#> SessionManager manager = new SessionManager();
return manager.Customer()。QueryAll();
你可以做这样的事情
公共类CustomerManager
{
私有SessionManager sessionManager;
public CustomerManager(SessionManager sessionManager)
{
this.sessionManager = sessionManager;
}
public ICustomerRetList QueryActive()
{
//使用this.sessionManager查询活跃客户并返回
}
}
公共静态类MyExtensions
{
公共静态CustomerManager客户(此SessionManager sessionManager)
{
返回新的CustomerManager(sessionManager);
}
}
然后使用它像
SessionManager manager = new SessionManager();
manager.Customers()。QueryActive();
您可以与供应商做同样的事情,所以有一个VendorManager类。
你不能使用复合名称创建扩展方法 - 事实上,你不能使用这样的复合名称创建任何方法。因此,虽然您可以拥有CustomerQueryAll,但您不能拥有Customer.QueryAll。看起来你真正想要的是扩展方法的专业化,所以你会有这样的东西:public static ICustomerRetList QueryAll(此客户客户)
{
// 在这里做点什么。
}那样,你的代码管理器.Customer.QueryAll( )因为你现在正在通过客户,所以会有效。
I am using a third party library that has a connection object called a SessionManager. The SessionManager allows you to create different types of requests for querying, adding or modding. I am writing some extension methods to try and make the coding faster as I tend to repeat code for a lot of projects when using this component. Here's the regular code that I would use to query a list of cusotmers:
SessionManager manager = new SessionManager();
manager.OpenConnection(filename);
IMsgSetRequest MsgRequest = SessionManager.CreateMsgSetRequest();
ICustomerQuery query = MsgRequest.AppendCustomerQuery();
query.ActiveStatus.SetValue(ENActiveStatus.All);
IResponse response = SessionManager.DoRequests(MsgRequest);
if(response.StatusCode == 0) { return (ICustomerRetList)response.Detail; }
else { throw new Exception("Unknown error querying customers: " + response.StatusMessage; }
I don't know if it's possible using extensions, but I would like to be able to be able to change my code to do something like this:
SessionManager manager = new SessionManager();
return manager.Customers.QueryAll();
The idea being that I could then code some standard queries like
manager.Customers.QueryActive();
and
manager.Vendors.QueryAll();
The hard part that I am not able to figure out is the part between the SessionManager and the actual extension function for the query. It doesn't seem like you can do a nested class with extensions.
What I have tried:
I've tried creating a static class within a static class, but I get the error that extension methods must be in a top level class. I tried the following code, but the compiler didn't seem to understand what I was trying:
public static ICustomerRetList Customer.QueryAll(this SessionManager manager)
You can't created "nested" extension methods, but your extension method can return an instance of a class / struct with other methods on it.
Try something like this:
public static class MyExtensions { public static CustomerWrapper Customer(this SessionManager session) { return new CustomerWrapper(session); } } public struct CustomerWrapper { public CustomerWrapper(SessionManager session) { Session = session; } public SessionManager Session { get; } public ICustomerRetList QueryAll() { ... } }
You'll just need to add parentheses after theCustomer
call:
SessionManager manager = new SessionManager(); return manager.Customer().QueryAll();
You can do something like this
public class CustomerManager { private SessionManager sessionManager; public CustomerManager(SessionManager sessionManager) { this.sessionManager = sessionManager; } public ICustomerRetList QueryActive() { // use this.sessionManager to query active customers and return them } } public static class MyExtensions { public static CustomerManager Customers(this SessionManager sessionManager) { return new CustomerManager(sessionManager); } }
then use it like
SessionManager manager = new SessionManager(); manager.Customers().QueryActive();
You can do the same thing with Vendors, so have a VendorManager class.
You can't create your extension methods using composite names - in fact, you can't create any method using a composite name like that. So, while you could have CustomerQueryAll, you can't have Customer.QueryAll. What it looks like you really want, is to have a specialization of the extension method so you would have something like this:public static ICustomerRetList QueryAll(this Customer customer) { // Do something here. }That way, your code manager.Customer.QueryAll() would work because you are passing the customer in now.
这篇关于具有嵌套名称的扩展名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!