Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/273.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/29.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# Asp.Net反序列化字典<;字符串,对象>;对一个物体_C#_Asp.net_Serialization - Fatal编程技术网

C# Asp.Net反序列化字典<;字符串,对象>;对一个物体

C# Asp.Net反序列化字典<;字符串,对象>;对一个物体,c#,asp.net,serialization,C#,Asp.net,Serialization,反序列化的最佳方式是什么 Dictionary<string, object>{ {"fName", "John"}, {"lName", "Doe"}, {"email", "john@doe.net"} } 这并不是正常意义上的真正序列化,通常是指获取一些磁盘或网络格式的数据(二进制、ASCII、JSON等)并将其加载到运行时对象中 然而,如果我理解正确的话,我想你想做的是 public Member( Dictionary<string,object&

反序列化的最佳方式是什么

Dictionary<string, object>{
   {"fName", "John"},
   {"lName", "Doe"},
   {"email", "john@doe.net"}
}

这并不是正常意义上的真正序列化,通常是指获取一些磁盘或网络格式的数据(二进制、ASCII、JSON等)并将其加载到运行时对象中

然而,如果我理解正确的话,我想你想做的是

public Member( Dictionary<string,object> dictionary )
{ 
    fName = dictionary["fName"];
    lName = dictionary["lName"];
    email = dictionary["email"];
}

// usage Member m = new Member( inputDictionary );
公共成员(字典)
{ 
fName=字典[“fName”];
lName=字典[“lName”];
电子邮件=字典[“电子邮件”];
}
//用法成员m=新成员(inputDictionary);

如果结构是静态的:

return new Member 
{
   fName = myDictionary["fName"], 
   lName = myDictionary["lName"], 
   email = myDictionary["email"]
};
如果结构在设计时未知:

public static T Hydrate<T>(this Dictionary<string, string> theDictionary, 
   T myObject = new T()) where T:new() //default/optional parameter is valid in 4.0 only
{

   //var myObject = myObject ?? new T(); //alternative in 3.5 and previous

   foreach(string key in myDictionary.Keys)
   {
      var propInfo = typeof(T).GetProperty(key);

      if(propInfo == null) throw new ArgumentException("key does not exist");
      propInfo.SetValue(myObject, theDictionary[key], null);
   }
   return myObject;
}
public static T Hydrate(这本字典是字典,
T myObject=new T()),其中T:new()//默认/可选参数仅在4.0中有效
{
//var myObject=myObject??新的T();//3.5和以前版本中的替代方案
foreach(myDictionary.Keys中的字符串键)
{
var-propInfo=typeof(T).GetProperty(键);
如果(propInfo==null)抛出新的ArgumentException(“键不存在”);
设置值(myObject,字典[key],null);
}
返回myObject;
}

一种似乎有意义的方法是为此提供一个静态帮助函数

public static Member Create(Dictionary<string, object inputs)
{
    Member oNew = new Member();
    oNew.fName = inputs["fName"].ToString();
    // etc
    return oNew;
}

publicstaticmembercreate(Dictionary您也可以对此使用反射,根据您实际使用的对象,可能会有很多代码,但它会更灵活。这可能不是一个完整的示例,但它可以让您了解一般的想法

public T InstantiateFromDictionary<T>(Dictionary<string, object> values) where T : new()
{
   Type t = typeof(T);
   T instance = new T();
   foreach(KeyValuePair<string, object> kvp in values)
   {
        PropertyInfo prop = t.GetProperty(kvp.Key);
        prop.SetValue(instance, kvp.Value, null);
   }

   return instance;
}
public T实例化字典(字典值),其中T:new()
{
类型t=类型(t);
T实例=新的T();
foreach(值中的KeyValuePair kvp)
{
PropertyInfo prop=t.GetProperty(kvp.Key);
prop.SetValue(实例,kvp.Value,null);
}
返回实例;
}

这不是序列化,而是转换。如果你真的想让它可转换,那就让它可转换

示例代码

using System;
使用System.Collections.Generic; 使用System.Linq; 使用系统文本; 使用System.IO; 使用System.Xml.Linq; 使用系统诊断; 使用System.Xml.Serialization; 使用系统组件模型; 利用制度全球化

命名空间控制台应用程序1 {

内部类成员转换器:类型转换器
{
公共覆盖布尔CanConvertFrom(ITypeDescriptorContext上下文,
类型(源类型)
{
if(sourceType==typeof(字典))
{
返回true;
}
返回base.CanConvertFrom(上下文,sourceType);
}
公共重写对象转换自(ITypeDescriptorContext上下文,
文化信息(文化,对象值)
{
if(值为字典)
{
成员=新成员();
字典d=(字典)值;
if(d.ContainsKey(“fName”){member.fName=Convert.ToString(d[“fName]”);};
if(d.ContainsKey(“lName”){member.lName=Convert.ToString(d[“lName]”);};
if(d.ContainsKey(“email”){member.email=Convert.ToString(d[“email”]);};
返回成员;
}
返回base.ConvertFrom(上下文、区域性、值);
}
公共重写对象转换为(ITypeDescriptorContext上下文,
CultureInfo区域性,对象值,类型destinationType)
{
if(destinationType==typeof(字典))
{
成员=(成员)值;
字典d=新字典();
d、 添加(“fName”,member.fName);
d、 添加(“lName”,member.lName);
d、 添加(“电子邮件”,member.email);
返回d;
}
返回base.ConvertTo(上下文、区域性、值、destinationType);
}
}
[TypeConverter(typeof(MemberConverter))]
内部班级成员
{
公共字符串fName;
公共字符串名称;
公共字符串电子邮件;
}
班级计划
{
静态void Main(字符串[]参数)
{
var d=新字典{
{“fName”,“John”},
{“lName”,“Doe”},
{“电子邮件”john@doe.net"}
};
成员m=(成员)TypeDescriptor.GetConverter(typeof(成员)).ConvertFrom(d);
Debugger.Break();
}
}

}

最近,NewtonSoft在ASP.NET应用程序中变得如此普遍,如果我需要一个简短而甜蜜的解决方案来实现这种映射,我可以使用一些类似的代码。当然,如果第三方库还不存在,引入第三方库可能是滥用,但有时我们只使用有效的。NewtonSoft就是这样做的

using Newtonsoft.Json;
class SerializationTests
{
    public void DictionarySerializeSample()
    {

        var dict = new Dictionary<string, object>
        {
            {"fName", "John"},
            {"lName", "Doe"},
            {"email", "john@doe.net"}
        };

        string dictInJson = JsonConvert.SerializeObject(dict);
        var member = JsonConvert.DeserializeObject<Member>(dictInJson);

        // use Newtonsoft to write out the object re-serialized
        Console.WriteLine(JsonConvert.SerializeObject(member, Formatting.Indented));

    }

    public class Member
    {
        public string fName;
        public string lName;
        public string email;
    }
}
使用Newtonsoft.Json;
类序列化测试
{
public void DictionarySerializeSample()
{
var dict=新字典
{
{“fName”,“John”},
{“lName”,“Doe”},
{“电子邮件”john@doe.net"}
};
字符串dictInJson=JsonConvert.SerializeObject(dict);
var member=JsonConvert.DeserializeObject(dictInJson);
//使用Newtonsoft写出重新序列化的对象
WriteLine(JsonConvert.SerializeObject(member,Formatting.Indented));
}
公共班级成员
{
公共字符串fName;
公共字符串名称;
公共字符串电子邮件;
}
}

我一直在考虑这个问题,但我不喜欢硬编码任何值的想法。这就是为什么我要问最好的方法,如果有什么可以避免这种方法的话。我只是在寻找这样的
Member=(Member)myDictionary;
我会将它放在
成员的构造函数中,该构造函数将字典作为参数。添加到答案中…如果您想避免硬编码键值,那么我会使用基于反射的技术,如@KiethSoffered@negative:因此项目总是3?或更多?如fName、lName、email和A例如,Address。@KMan,是的,它会更多,
Member
类作为一个例子。我想我的类肯定有问题,因为
typeof(T)。GetProperty(key)
总是空的和
internal class MemberConverter : TypeConverter
{
    public override bool CanConvertFrom(ITypeDescriptorContext context,
  Type sourceType)
    {
        if (sourceType == typeof(Dictionary<string, object>))
        {
            return true;
        }
        return base.CanConvertFrom(context, sourceType);
    }

    public override object ConvertFrom(ITypeDescriptorContext context,
  CultureInfo culture, object value)
    {
        if (value is Dictionary<string, object>)
        {
            Member member = new Member();
            Dictionary<string, object> d = (Dictionary<string, object>)value;
            if (d.ContainsKey("fName")) { member.fName = Convert.ToString(d["fName"]); };
            if (d.ContainsKey("lName")) { member.lName = Convert.ToString(d["lName"]); };
            if (d.ContainsKey("email")) { member.email = Convert.ToString(d["email"]); };
            return member;
        }
        return base.ConvertFrom(context, culture, value);
    }

    public override object ConvertTo(ITypeDescriptorContext context,
  CultureInfo culture, object value, Type destinationType)
    {
        if (destinationType == typeof(Dictionary<string, object>))
        {
            Member member = (Member)value;
            Dictionary<string, object> d = new Dictionary<string, object>();
            d.Add("fName", member.fName);
            d.Add("lName", member.lName);
            d.Add("email", member.email);
            return d;
        }
        return base.ConvertTo(context, culture, value, destinationType);
    }

}

[TypeConverter(typeof(MemberConverter))]
internal class Member
{
    public string fName;
    public string lName;
    public string email;
}

class Program
{
    static void Main(string[] args)
    {
        var d = new Dictionary<string, object> {
           {"fName", "John"},
           {"lName", "Doe"},
           {"email", "john@doe.net"}
        };

        Member m = (Member)TypeDescriptor.GetConverter(typeof(Member)).ConvertFrom(d);

        Debugger.Break();
    }
}
using Newtonsoft.Json;
class SerializationTests
{
    public void DictionarySerializeSample()
    {

        var dict = new Dictionary<string, object>
        {
            {"fName", "John"},
            {"lName", "Doe"},
            {"email", "john@doe.net"}
        };

        string dictInJson = JsonConvert.SerializeObject(dict);
        var member = JsonConvert.DeserializeObject<Member>(dictInJson);

        // use Newtonsoft to write out the object re-serialized
        Console.WriteLine(JsonConvert.SerializeObject(member, Formatting.Indented));

    }

    public class Member
    {
        public string fName;
        public string lName;
        public string email;
    }
}