【问题标题】:Using reflection to set properties for wcf datacontract使用反射设置 wcf 数据契约的属性
【发布时间】:2013-11-30 17:04:40
【问题描述】:

我有一个 wcf 服务,在客户端通过 wsdl 文件动态调用 wcf 服务。现在我想在我的 wcf 服务中设置方法的属性。我有复杂类型的属性,在每个复杂类型中我有 30 到 4o 个复杂类型。我无法触及服务,唯一的事情是使用反射将值分配给服务数据合同并使用 methodInfo.Invoke(传递构造的对象数组)。客户端将在字典中传递数据合同的输入参数。递归是否需要在复杂类型的内部类中导航并设置值。 示例代码:

 public CompositeType GetTestDataUsingDataContract(CompositeType composite, string str, int i,EmployeeIn obj)
 {
        ///code here
 }
  **DataContract for CompositeType**
      [DataContract]
public class CompositeType
{
    bool boolValue = true;
    string stringValue = "Hello ";
    EmployeeIn employeeValue;
    [DataMember]
    public bool BoolValue
    {
        get { return boolValue; }
        set { boolValue = value; }
    }

    [DataMember]
    public string StringValue
    {
        get { return stringValue; }
        set { stringValue = value; }
    }
    [DataMember]
    public EmployeeIn EmploValue
    {
        get { return employeeValue; }
        set { employeeValue = value; }
    }
}  
   EmployeeIn Class
       [DataContract]
public class EmployeeIn
{
    bool UserIsOnline = true;
    string UserName = "DANGER";

    [DataMember]
    public bool BoolValue
    {
        get { return UserIsOnline; }
        set { UserIsOnline = value; }
    }

    [DataMember]
    public string StringValue
    {
        get { return UserName; }
        set { UserName = value; }
    }
}

使用反射我想设置这些属性。

【问题讨论】:

    标签: c# wcf reflection


    【解决方案1】:

    这是一个通过反射设置属性的工作代码,属性名称由字符串提供:

    void SetPropertyValue(object obj, string propertyPath, object value)
    {
        System.Reflection.PropertyInfo currentProperty = null;
        string[] pathSteps = propertyPath.Split('/');
        object currentObj = obj;
        for (int i = 0; i < pathSteps.Length; ++i)
        {
            Type currentType = currentObj.GetType();
            string currentPathStep = pathSteps[i];
            var currentPathStepMatches = Regex.Match(currentPathStep, @"(\w+)(?:\[(\d+)\])?");
            currentProperty = currentType.GetProperty(currentPathStepMatches.Groups[1].Value);
            int arrayIndex = currentProperty.PropertyType.IsArray ? int.Parse(currentPathStepMatches.Groups[2].Value) : -1;
            if (i == pathSteps.Length - 1)
            {
                currentProperty.SetValue(currentObj, value, arrayIndex > -1 ? new object[] { arrayIndex } : null);
            }
            else
            {
                if (currentProperty.PropertyType.IsArray)
                {
                    currentObj = (currentProperty.GetValue(currentObj) as Array).GetValue(arrayIndex);
                }
                else
                {
                    currentObj = currentProperty.GetValue(currentObj);
                }
            }
        }
    }
    

    现在您可以使用此函数来设置属性,例如:

    SetPropertyValue(someClass, "SomeInt", 4);
    SetPropertyValue(someClass, "ArrayProperty[5]/Char", (char)2);
    SetPropertyValue(someClass, "TestField", new SomeOtherClass());
    

    【讨论】:

    • 您好 Konrad,我已经编写了我的代码,请提供您的输入,
    • @user3001558 如果您觉得我的功能有帮助并希望得到澄清,请根据我的功能的使用情况更新您的答案。
    • 谢谢,但是我可以在线程中找到我的代码和你的代码,我应该将压缩的代码文件发送到你的 id,请提供你的邮件 ID。我们正在开发集成 50 diffr 的 POC。 wcf 和网络服务。提供通用溶胶。
    • @user3001558 为了完整性,最好在这里展示你的努力。
    猜你喜欢
    • 1970-01-01
    • 2011-01-25
    • 1970-01-01
    • 1970-01-01
    • 2010-10-11
    • 1970-01-01
    • 2021-02-21
    相关资源
    最近更新 更多