0
source中授權狀態的實現表明授權狀態應該是可序列化的,但是當我嘗試序列化授權狀態以存儲在我的身份驗證票證中時,一個錯誤說實現需要一個無參數的構造函數。AuthorizationState無法序列化,因爲它沒有無參數的構造函數
從引用源剪斷:
[Serializable]
public class AuthorizationState : IAuthorizationState {
...
public AuthorizationState(IEnumerable<string> scopes = null) {
我應得的狀態:
var Authorization = Client.ProcessUserAuthorization();
如何,我試圖序列狀態:
public string Serialize(IAuthorizationState objectInstance)
{
var serializer = new XmlSerializer(objectInstance.GetType());
var sb = new StringBuilder();
using (TextWriter writer = new StringWriter(sb))
{
serializer.Serialize(writer, objectInstance);
}
return sb.ToString();
}
我跑了一個快速控制檯應用程序
class Program
{
static void Main(string[] args)
{
var can = new CanSerialize();
var cant = new CantSerialize();
var ser1 = can.Serialize();
var ser2 = cant.Serialize();
}
}
[Serializable]
public class CantSerialize
{
public int id { get; set; }
public CantSerialize(int ID = 1)
{
id = ID;
}
public string Serialize()
{
var serializer = new XmlSerializer(this.GetType());
var sb = new StringBuilder();
using (TextWriter writer = new StringWriter(sb))
{
serializer.Serialize(writer, this);
}
return sb.ToString();
}
}
[Serializable]
public class CanSerialize
{
public int id { get; set; }
private CanSerialize()
{
id = 1;
}
public CanSerialize(int ID = 1)
{
id = ID;
}
public string Serialize()
{
var serializer = new XmlSerializer(this.GetType());
var sb = new StringBuilder();
using (TextWriter writer = new StringWriter(sb))
{
serializer.Serialize(writer, this);
}
return sb.ToString();
}
}
該實現是否可以有一個默認的無參數構造函數?正如樣本所顯示的,如果有人擔心有公共事務人員可能是私人的。
與此同時,我將創建一個數據傳輸對象來在我的授權令牌和狀態之間來回傳遞信息。
編輯: 我發現JSON.net轉換效果很好:
var a = JsonConvert.SerializeObject(Authorization);
var b = JsonConvert.DeserializeObject(a, typeof(AuthorizationState));