Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/305.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#实现方式同步功能_C#_Visual Studio_Function_Inverse - Fatal编程技术网

C#实现方式同步功能

C#实现方式同步功能,c#,visual-studio,function,inverse,C#,Visual Studio,Function,Inverse,我这样做: businessCRM.Description = string.Format("{0}\n" + "Name: {1}\n" + "Street: {2}\n " + "Number: {3}\n" + "floor: {4}\n" + business.Name, business.Address.Street, business.

我这样做:

businessCRM.Description = 
    string.Format("{0}\n" +
             "Name: {1}\n" +
             "Street: {2}\n " +
             "Number: {3}\n" +
             "floor: {4}\n" +
      business.Name,
      business.Address.Street,
      business.Address.Number,
      business.Address.floor,
);
我想做同样的事情,但是用另一种方法做反向的,类似这样的事情来做方式同步(往返):


但是我不知道这是否完全正确,你知道吗?

真正的解决方案是在
businessCRM
类中添加一个
地址
成员,这样你就可以存储数据和描述,而不必在以后从描述中提取数据。解析它只会导致问题。

您可以使用下面的方法将其拆分到字典中;注意,我必须对第一行进行特殊处理(没有前缀),如果您的数据在值中内部包含换行符,则无法正常工作:

var parts = new Dictionary<string, string>(
    StringComparer.InvariantCultureIgnoreCase);
string firstLine;
using(var reader = new StringReader(description))
{
    string line;
    firstLine = reader.ReadLine();
    var splitBy = new[] { ':' };
    while ((line = reader.ReadLine()) != null)
    {
        var pair = line.Split(splitBy, 2, StringSplitOptions.None);
        if (pair.Length == 2) parts[pair[0].Trim()] = pair[1].Trim();
    }
}
string tmp;

string name, street, number, floor; // in your case, you could assign to
                                    // the properties directly
name = parts.TryGetValue("Name", out tmp) ? tmp : "";
street = parts.TryGetValue("Street", out tmp) ? tmp : "";
number = parts.TryGetValue("Number", out tmp) ? tmp : "";
floor = parts.TryGetValue("floor", out tmp) ? tmp : "";
var parts=新字典(
StringComparer.InvariantCultureInogoreCase);
字符串第一行;
使用(变量读取器=新的StringReader(说明))
{
弦线;
firstLine=reader.ReadLine();
var splitBy=new[]{':'};
而((line=reader.ReadLine())!=null)
{
var pair=line.Split(splitBy,2,StringSplitOptions.None);
如果(pair.Length==2)零件[pair[0].Trim()]=pair[1].Trim();
}
}
串tmp;
字符串名称、街道、编号、楼层;//在您的情况下,您可以将
//属性直接
名称=零件.TryGetValue(“名称”,输出tmp)?tmp:”;
街道=零件。TryGetValue(“街道”,超出tmp)?tmp:”;
编号=零件。TryGetValue(“编号”,输出tmp)?tmp:”;
地板=零件。TryGetValue(“地板”,超出tmp)?tmp:”;

首先,请看@Marc gravel的评论——处理“反序列化”会容易得多。 如果不是,这里是解决方案

  • 我的代码可能很慢-您需要缓存反射信息(如属性信息集合)
  • 没有错误处理和检查(例如为空-手动执行;)
  • 代码假定输入始终是正确的输入(例如,值内的新行字符)、格式等
  • 代码:


    String.format的使用完全错误。你的格式在哪里?更重要的是,你不能简单地用“invere”来表示字符串。您需要首先对其进行比较。首先,您的
    字符串。Format
    是错误的,因为第一个参数是Format string。第二:如果您需要单独访问它,为什么要合并它?我更新了代码,您可以看到我现在拥有的代码。为什么您需要从描述中解析它?您的描述可能包含类似json的内容吗?那么这将是微不足道的事情。你在这里寻找的单词可能是“序列化”,顺便说一句-不是“同步”,你能举个例子吗?@user1911如果没有看到你的代码,例子是不可能的。您从未向类声明businessCRM是什么。发布更多代码,我会尝试一下。顺便说一句:
    //在您的情况下,您可以直接分配属性
    -这是错误的-不能将
    out
    与对象属性一起使用,我的意思是:
    myMethod(“Name”,out myObj.Name)
    -导致编译时错误。@pw您误解了;我的意思是“代替变量”,即
    business.Address.Street=parts.TryGetValue(“Street”,out tmp)?tmp:”etc@NMarcGravell啊,我的错。
    
    var parts = new Dictionary<string, string>(
        StringComparer.InvariantCultureIgnoreCase);
    string firstLine;
    using(var reader = new StringReader(description))
    {
        string line;
        firstLine = reader.ReadLine();
        var splitBy = new[] { ':' };
        while ((line = reader.ReadLine()) != null)
        {
            var pair = line.Split(splitBy, 2, StringSplitOptions.None);
            if (pair.Length == 2) parts[pair[0].Trim()] = pair[1].Trim();
        }
    }
    string tmp;
    
    string name, street, number, floor; // in your case, you could assign to
                                        // the properties directly
    name = parts.TryGetValue("Name", out tmp) ? tmp : "";
    street = parts.TryGetValue("Street", out tmp) ? tmp : "";
    number = parts.TryGetValue("Number", out tmp) ? tmp : "";
    floor = parts.TryGetValue("floor", out tmp) ? tmp : "";
    
    var addressType = business.Address.GetType();
    
    foreach (var line in businessCRM.Description
                                    .Split(new[] { "\n", Environment.NewLine },
                                                  StringSplitOptions.RemoveEmptyEntries))
    {
        var propSelectorIndex = line.IndexOf(":");
        if (propSelectorIndex == -1) continue;
    
        var propName = line.Subtring(0, propSelectorIndex);
        var propInfo = addressType.GetProperties(BindigsFlag.Public 
                                                          | BindigsFlag.Instance)
                                  .FirstOrDefault(prop => prop.Name == propName);
    
        if (propInfo == null) throw new InvalidOperationException();
    
        var newPropValue = line.Substring(propSelectorIndex + 2); 
                     // + 2 to omit : char and additional space
        propInfo.SetValue(business.Address, newPropValue, null);
    }