[fix]修正UdpServer在接收广播时连续启动接收的错误,在StarAgent中,此时可能收到广播包,SocketFlags是Broadcast,需要清空,否则报错“参考的对象类型不支持尝试的操作”; 无需设置SocketOptionName.PacketInformation,在ReceiveMessageFromAsync时会自动设置,并且支持ipv6;
石头 编写于 2024-10-10 00:36:00 石头 提交于 2024-10-10 00:45:43
X
using System.Dynamic;
using System.Xml.Linq;

namespace NewLife.Reflection;

/// <summary>动态Xml</summary>
public class DynamicXml : DynamicObject
{
    /// <summary>节点</summary>
    public XElement? Node { get; set; }

    /// <summary>实例化</summary>
    public DynamicXml() { }

    /// <summary>实例化</summary>
    /// <param name="node"></param>
    public DynamicXml(XElement node) => Node = node;

    /// <summary>实例化</summary>
    /// <param name="name"></param>
    public DynamicXml(String name) => Node = new XElement(name);

    /// <summary>设置</summary>
    /// <param name="binder"></param>
    /// <param name="value"></param>
    /// <returns></returns>
    public override Boolean TrySetMember(SetMemberBinder binder, Object? value)
    {
        if (Node == null) throw new ArgumentNullException(nameof(Node));
        if (value == null) throw new ArgumentNullException(nameof(value));

        var setNode = Node.Element(binder.Name);
        if (setNode != null)
            setNode.SetValue(value);
        else
        {
            if (value.GetType() == typeof(DynamicXml))
                Node.Add(new XElement(binder.Name));
            else
                Node.Add(new XElement(binder.Name, value));
        }

        return true;
    }

    /// <summary>获取</summary>
    /// <param name="binder"></param>
    /// <param name="result"></param>
    /// <returns></returns>
    public override Boolean TryGetMember(GetMemberBinder binder, out Object? result)
    {
        if (Node == null) throw new ArgumentNullException(nameof(Node));

        result = null;
        var getNode = Node.Element(binder.Name);
        if (getNode == null) return false;

        result = new DynamicXml(getNode);
        return true;
    }

#if DEBUG
    // 2019-03-10合并为混合项目后一下编译不通过,注释掉
    ///// <summary>测试</summary>
    //public static void Test()
    //{
    //    dynamic xml = new DynamicXml("Test");
    //    xml.Name = "NewLife";
    //    xml.Sign = "学无先后达者为师!";
    //    xml.Detail = new DynamicXml();
    //    xml.Detail.Name = "新生命开发团队";
    //    xml.Detail.CreateTime = new DateTime(2002, 12, 31);

    //    var node = xml.Node as XElement;
    //    var str = node.ToString();
    //    Console.WriteLine(str);
    //}
#endif
}