win8x64 ,vs2010,Framworkv4.0;
代码:
[DataContract]
public class User
{
[DataMember(Order=0,IsRequired=true)]
public string name { get; set; }
[DataMember(Order=1)]
public string email { get; set; }
[DataMember(Order = 2)]
public string address { get; set; }
[DataMember(Order = 3)]
public string sex { get; set; }
}
private void button1_Click(object sender, EventArgs e)
{
User user=new User{
name=textBox1.Text,
email=textBox2.Text,
address=textBox3.Text,
sex=radioButton1.Checked?"男":"女"
};
var json = JsonConvert.SerializeObject(user);
。。。。
错误堆栈:
未处理 System.Security.VerificationException
HResult=-2146233075
Message=操作可能会破坏运行时稳定性。
Source=Newtonsoft.Json
StackTrace:
在 Newtonsoft.Json.Serialization.DefaultContractResolver.SetPropertySettingsFromAttributes(JsonProperty property, Object attributeProvider, String name, Type declaringType, MemberSerialization memberSerialization, Boolean& allowNonPublicAccess)
在 Newtonsoft.Json.Serialization.DefaultContractResolver.CreateProperty(MemberInfo member, MemberSerialization memberSerialization)
在 Newtonsoft.Json.Serialization.DefaultContractResolver.CreateProperties(Type type, MemberSerialization memberSerialization)
在 Newtonsoft.Json.Serialization.DefaultContractResolver.CreateObjectContract(Type objectType)
在 Newtonsoft.Json.Serialization.DefaultContractResolver.CreateContract(Type objectType)
。。。
在另外一个项目中出现类似的问题。
代码:
using Couchbase;
using Couchbase.Extensions;
using Enyim.Caching.Memcached;
using Couchbase.Configuration;
//读配置文件的方式创建client
[System.Security.Permissions.PermissionSet(System.Security.Permissions.SecurityAction.Assert)]
private static void NewMethod1()
{
var client = CouchbaseManager.Instance;
var key = "cottrell_brewing-old_yankee_ale";
var newBeer = new Beer
{
Name = "Old Yankee Ale",
abv = 5.00f,
BreweryId = "cottrell_brewing",
Style = "American-Style Amber",
Category = "North American Ale"
};
var result = client.StoreJson(StoreMode.Add, key, newBeer);
if (result)
{
var savedBeer = client.GetJson<Beer>(key);
Console.WriteLine("Found beer:" + savedBeer.Name);
}
}
[Serializable]
public class Beer
{
[JsonProperty("name")]
public string Name { get; set; }
[JsonProperty("abv")]
public float abv { get; set; }
[JsonProperty("type")]
public string Yype { get { return "beer"; } }
[JsonProperty("brewery_id")]
public string BreweryId { get; set; }
[JsonProperty("style")]
public string Style { get; set; }
[JsonProperty("category")]
public string Category { get; set; }
}
异常详细信息:
var json = JsonConvert.SerializeObject(user); JSON已经是文本了。你直接用ns.write写入就好了。没必要在Serialize了
感谢热心的回答,但问题没有得到解决。。。
执行“var json = JsonConvert.SerializeObject(user);”时已经报错了
@MagicJie: 那就贴你的USER代码
@56180825: 已补上。谢谢