我正在尝试为内部存储数据的集合编写接口 JObject
internal class JsonDataSet : IDataSet
{
private JObject Document { get; set; }
// The following methods are from the IDataSet interface
public int Count { ... }
public void Add<T>(string key, T value) { ... }
public T GetItem<T>(string key) { ... }
public bool ContainsKey(string key) { ... }
}
Run Code Online (Sandbox Code Playgroud)
在Add<T>方法中,如果自定义类型没有DataContract注释,我想提供一个有用的例外.例如,如果有人打电话:
dataSet.Add<IDictionary<string, IList<CustomType>>>(dict);
Run Code Online (Sandbox Code Playgroud)
"Cannot serialize type 'CustomType'. DataContract annotations not found."如果CustomType没有正确的注释,它将抛出异常.
到目前为止,我已经找到了一种方法来获取类型定义中的每个泛型参数,以便我可以检查它们:
private IEnumerable<Type> GetGenericArgumentsRecursively(Type type)
{
if (!type.IsGenericType) …Run Code Online (Sandbox Code Playgroud)