C# 如何使这个System.Serializable类不可知?

C# 如何使这个System.Serializable类不可知?,c#,unity3d,C#,Unity3d,原则如下: 我有很多Ajax交换 我有一个系统。Serializable类是Ajax调用的结果 我总是有两个功能: 调用的函数 用于分析调用的函数 进行调用的函数获取回调作为参数,进行调用,调用完成后,使用System.Serializable类回调 下面是一个例子: [System.Serializable] public class JsonState { public int state; public string message; public Json

原则如下:

  • 我有很多Ajax交换
  • 我有一个
    系统。Serializable
    类是Ajax调用的结果
  • 我总是有两个功能:
    • 调用的函数
    • 用于分析调用的函数
进行调用的函数获取回调作为参数,进行调用,调用完成后,使用
System.Serializable
类回调

下面是一个例子:

[System.Serializable]
public class JsonState
{
    public int state;
    public string message;
    public JsonData data;
}
在我的课堂上:

public class DataController : MonoBehaviour {
    public delegate void CallbackAjaxStateFinished(JsonState j);
    public CallbackAjaxStateFinished cbAjaxStateFinished = null;

    public void AjaxStateGet(CallbackAjaxStateFinished cb=null)
    {
        /* make the Ajax call*/
        cbAjaxStateFinished = cb;
        new HTTPRequest(
            new Uri(baseURL + _urlGetState),
            HTTPMethods.Get, AjaxStateGetFinished
        ).Send();
    }

    private void AjaxStateGetFinished(
        HTTPRequest request, HTTPResponse response
    ) {
        if (response == null) {
            return;
        }
        JsonState j = null;
        try {
            j = JsonUtility.FromJson<JsonState>(response.DataAsText);
            if (cbAjaxStateFinished != null) {
                cbAjaxStateFinished(j);
            }
        } catch (ArgumentException) { /* Conversion problem */
            TryDisplayFatalError(FatalErrors[FatalError.ConversionProblem2]);
        }
    }
}
但我将添加另一个,并复制/粘贴代码以发送/接收上述内容,只需2个小修改:

  • 更改
    CallbackXxx
    类型
  • 更改
    System.Serializable

我是C#的新手,有没有办法让这个通用化?

这个过程没有那么复杂,假设您的回调总是采用
void(SomeStateclass)
的形式

在上面的代码中将
JSonState
替换为泛型T就可以了。这就是你最终的结果:

// The DataController takes a generic T respresenting one of your State classes
// notice that we need a constraint for the type T to have a parameterless constructor
public class DataController<T> : MonoBehaviour where T: new()
{ 
    // the delegate takes the generic type, so we only need one
    public delegate void CallbackAjaxFinished(T j);

    public CallbackAjaxFinished cbAjaxFinished = null;

    public void AjaxStateGet(CallbackAjaxFinished cb=null)
    {
        /* make the Ajax call*/
        cbAjaxFinished = cb;
        new HTTPRequest(
            new Uri(baseURL + _urlGetState),
            HTTPMethods.Get, AjaxStateGetFinished
        ).Send();
    }

    private void AjaxStateGetFinished(
        HTTPRequest request, HTTPResponse response
    ) {
        if (response == null) {
            return;
        }
        // here we use the default keyword to get 
        // an instance of an initialized stronglytyped T
        T j = default(T);
        try {
            // the T goes into the FromJson call as that one was already generic
            j = JsonUtility.FromJson<T>(response.DataAsText);
            if (cbAjaxFinished != null) {
                cbAjaxFinished(j);
            }
        } catch (ArgumentException) { /* Conversion problem */
            TryDisplayFatalError(FatalErrors[FatalError.ConversionProblem2]);
        }
    }
}
//DataController使用一个泛型T来表示您的一个状态类
//注意,类型T需要一个约束才能有一个无参数构造函数
公共类DataController:MonoBehavior,其中T:new()
{ 
//委托采用泛型类型,因此我们只需要一个
公共代表无效CallbackAjaxFinished(T j);
public CallbackAjaxFinished cbAjaxFinished=null;
public void AjaxStateGet(CallbackAjaxFinished cb=null)
{
/*进行Ajax调用*/
cbAjaxFinished=cb;
新的HTTPRequest(
新Uri(baseURL+_urlGetState),
HTTPMethods.Get,AjaxStateGetFinished
).Send();
}
私有无效AjaxStateGetFinished(
HTTPRequest请求,HTTPResponse响应
) {
如果(响应==null){
返回;
}
//这里我们使用默认关键字来获取
//初始化的强类型T的实例
T j=默认值(T);
试一试{
//T进入FromJson调用,因为该调用已经是泛型的
j=JsonUtility.FromJson(response.DataAsText);
如果(cbAjaxFinished!=null){
cbajax(j);
}
}捕获(ArgumentException){/*转换问题*/
TryDisplayFatalError(FatalErrors[FatalError.ConversionProblem2]);
}
}
}
仅此而已。您的datacontroller类现在是泛型的

您可以这样使用它:

var dc = new DataController<JsonState>()
dc.AjaxStateGet( (v) => {
   v.Dump("jsonstate");
});

var dc2 = new DataController<JsonState2>();
dc2.AjaxStateGet( (v) => {
   v.Dump("jsonstate2");
});
var dc=new DataController()
dc.AjaxStateGet((v)=>{
v、 转储(“jsonstate”);
});
var dc2=新的DataController();
dc2.AjaxStateGet((v)=>{
v、 转储(“jsonstate2”);
});

关于C,我有很多问题要问,但你的回答解决了这些问题<代码>;^)非常感谢!
var dc = new DataController<JsonState>()
dc.AjaxStateGet( (v) => {
   v.Dump("jsonstate");
});

var dc2 = new DataController<JsonState2>();
dc2.AjaxStateGet( (v) => {
   v.Dump("jsonstate2");
});