将通用类T存储在变量中,然后在子方法

时间:2018-06-22 08:20:09

标签: c#

将通用类T存储在变量中,然后在子方法中重用它。

对于带有少量对象的WebService:

Foo:        Bar:        Etc..
SetFoo      SetBar      SetEtc
GetFoo      GetBar      GetEtc
UpdateFoo   UpdateBar   UpdateEtc
DeleteFoo   DeleteBar   DeleteEtc
GetList     ..          ..
GetPending  ..          ..
Processed   ..          ..

我在客户端使用以下单例通用包装,方法如下:

public bool Get<T>(int i, out DloExtention result)
// DloExtention is an interface implemented by foo, bar, etc..
{
    result = null;
    try
    {
        if (typeof(T) == typeof(Foo))
        {
            result = WebserviceClient.GetFoo(i);
        }
        else if (typeof(T) == typeof(Bar))
        {
            result = WebserviceClient.GetBar(i);
        }
        else if (typeof(T) == typeof(Etc))
        {
            result = WebserviceClient.GetEtc(i);
        }
        else
        {
            throw new NotSupportedException("Get<T>, T is not a supported type.");
        }
    }
    catch (Exception ex)
    {
        Log4N.Logger.Error($"Error in Namespace.ClientSide.Get<{nameof(T)}>(int {i} ). " + ex.Message);
        return false;
    }
    return true;
}

所以我可以使用同一个通用对象简单地处理所有类型:

class Processor
{
    HashSet<int> validOperation = new HashSet<int>();
    HashSet<int> invalidOperation = new HashSet<int>();

    internal void Run<T>()
    {
        if (Wrapper.Instance.GetListPending<T>(out int[] newEntityList) && newEntityList.Any())
        {
            ProcessEntities<T>(newEntityList, false);
        }
    }

    private void ProcessEntities<T>(int[] idsEnt, bool singleMode)
    {
        foreach (var idEnt in idsEnt)
        {
            ProcessEntity<T>(idEnt, false);
        }

        CloseValidOperation();
        RemoveInvalidOperation();
    }

    internal void ProcessIncident<T>(int idEnt)
    {
        if (Wrapper.Instance.Get<T>(idEnt, out LanDataExchangeCore.LanDataExchangeWCF.DloExtention currentEntity))
        {
            if (currentEntity.isValid() && currentEntity.toLocalDB())
            {
                validOperation.Add(idEnt);
            }
            else
            {
                invalidOperation.Add(idEnt);
            }
        }
    }

Wrapper.Instance.Get<T>Wrapper.Instance.GetListPending<T>需要通用参数。 但是,每一种方法都只需要使用它就能将<T>传递到最后一个方法。

是否可以将<T>调用中的Run<T>保存到私有变量中,以便类的内部方法可以使用它?

我尝试添加一个Type myType;,但是找不到在通用调用中使用它的方法。 Wrapper.Instance.Get<T>

的示例
Type myType; // class property

var fooWrapperGet = typeof(Wrapper).GetMethod("Get");
var fooOfMyTypeMethod = fooWrapperGet.MakeGenericMethod(new[] { myType });
//fooOfMyTypeMethod.Invoke(Wrapper.Instance , new object[] { new myType() });

// fooWrapperGet, as my wrapper is a singleton, Wrapper dont exposed Get<T>, but Wrapper.instance will expose it. 
// new myType()  <- do not compile. 

1 个答案:

答案 0 :(得分:2)

这对您有用吗?

private Dictionary<Type, Func<int, DloExtention>> gets =
    new Dictionary<System.Type, Func<int, DloExtention>>()
    {
        { typeof(Foo), WebserviceClient.GetFoo },
        { typeof(Bar), WebserviceClient.GetBar },
        { typeof(Etc), WebserviceClient.GetEtc },
    };

public bool Get<T>(int i, out DloExtention result)
{
    result = null;
    var flag = false;
    if (gets.ContainsKey(typeof(T)))
    {
        result = gets[typeof(T)](i);
        flag = true;
    }
    return flag;
}

这样做的好处是您可以在运行时填充字典。