简体   繁体   English

C#泛型-我可以将此类变成泛型吗?

[英]C# Generics - Can I make this class into a generic?

I have a class repeated many times. 我有一堂课重复了很多遍。 My boss ask me to make to generic but I'm not sure what this means. 我的老板要我做普通药,但是我不确定这意味着什么。 Can anyone out help or suggest how? 任何人都可以帮忙或提出建议吗? Sorry for bad English. 对不起,英语不好。 I hope it make sense. 我希望这是有道理的。

public class CapacityServiceContext : TableServiceContext
{
    public CapacityServiceContext(string baseAddress, StorageCredentials credentials)
        : base(baseAddress, credentials)
    {
    }
    public const string TableName = "Capacity";
    public IQueryable<Capacity> CapacityTable
    {
        get
        {
            return this.CreateQuery<Capacity>(TableName);
        }
    }

}

thank you in advance. 先感谢您。

Mina 米娜

As an example, your code could probably look like this: 例如,您的代码可能如下所示:

public class ServiceContext<T> : TableServiceContext
{
    public ServiceContext(string baseAddress, StorageCredentials credentials)
        : base(baseAddress, credentials)
    {
    }
    public const string TableName = typeof(T).Name;
    public IQueryable<T> Table
    {
        get
        {
            return this.CreateQuery<T>(TableName);
        }
    }
}

And would be instantiated like any generic class: 并且将像任何泛型类一样实例化:

ServiceContext<Capacity> context = new ServiceContext<Capacity>(…);

Essentially, I have changed every occurrence of Capacity in your class into T and made the class generic by appending <T> to the class name. 本质上,我已将类中每次出现的Capacity都更改为T并通过在类名后附加<T>来使类具有通用性。 Now the class is usable for other types as well. 现在,该类也可用于其他类型。

You can make TableServiceContext<T> , but your problem is going to be with your TableName constant. 您可以创建TableServiceContext<T> ,但是问题将出在TableName常量上。 Unless you can guarantee that the table name is always the same name as the class of your generic, then it doesn't look good. 除非可以保证表名始终与泛型的类名相同,否则它看起来就不好了。

But if you can make that guarantee, it would look something like this: 但是,如果可以做出保证,它将看起来像这样:

public class TableServiceContext<T>
{
    public TableServiceContext(string baseAddress, StorageCredentials credentials)
        : base(baseAddress, credentials)
    {
    }
    public string TableName { get { return typeof(T).Name; } }
    public IQueryable<T> Table
    {
        get
        {
            return this.CreateQuery<T>(TableName);
        }
    }
}

Generics are very popular and can help make your code more reusable. 泛型非常流行,可以使您的代码更可重用。

Here is a general overview, you can decide how to best use it in your projects. 这里是一般概述,您可以决定如何在项目中最好地使用它。

http://msdn.microsoft.com/en-us/library/512aeb7t(v=VS.100).aspx http://msdn.microsoft.com/zh-CN/library/512aeb7t(v=VS.100).aspx

I would gues your boss wants something like the following: 我会猜测您的老板想要以下内容:

public class ServiceContext<T> : TableServiceContext
{
    public ServiceContext(string baseAddress, StorageCredentials credentials)
        : base(baseAddress, credentials)
    {
    }
    public IQueryable<T> Table
    {
        get
        {
            return this.CreateQuery<T>(typeof(T).Name);
        }
    }

}

You should ask him/her to be sure though, you'd use this class as follows: 您应该要求他/她确定,但是您可以按以下方式使用此类:

ServiceContext<Capacity> serviceContext = new ServiceContext<Capacity>();
IQueryable<Capacity> query = serviceContext.Table;

I'm assuming he is suggesting that you make it generic with regard to Capacity. 我假设他建议您在容量方面将其设为通用。

public class ServiceContext<T> : TableServiceContext {

...

public IQueryable<T> Table {        
  get {            
    return this.CreateQuery<T>(typeof(T).Name);       
  }    
}

Then you could create a: 然后,您可以创建一个:

ServiceContext<Capacity>()

Without seeing the other classes, I'm not sure what would really be involved. 如果不看其他课程,我不确定会涉及到什么。

Possible he asked about changing CapacityTable property: 他可能询问有关更改CapacityTable属性的问题:

...
public IQueryable<T> Table
{
    get
    {
        return this.CreateQuery<T>(typeof(T).Name);
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM