⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 propertypackage.cs

📁 短信串口通信 于短信网关通信协议的封装 C#实现的类库
💻 CS
字号:
using System;
using System.Collections.Generic;
using System.Text;
using System.Reflection;

namespace JeasonZhao.Sms.SGIP.Command
{
    public enum PackageDataTypes
    {
        Uint,
        String,
        Byte,
    }
    public class PropertyPackage
    {
        #region members
        private PropertyInfo m_pInfo = null;
        private string m_strValue = null;
        private PackageDataTypes m_PackageDataType = PackageDataTypes.String;
        private uint m_IntValue = 0;
        private SmsFieldAttribute m_attribute = null;
        private uint m_nLength = 0;
        private BaseCommand m_command = null;
        #endregion
        public PropertyPackage(BaseCommand cmd, PropertyInfo pInfo, SmsFieldAttribute smsAttr)
        {
            if (null == pInfo || null == smsAttr)
            {
                return;
            }
            m_pInfo = pInfo;
            m_attribute = smsAttr;
            m_command = cmd;
            ReBuildPackage();
        }
        #region Get Functions
        public uint Length
        {
            get { return m_nLength; }
        }
        public SmsFieldAttribute Attribute
        {
            get { return m_attribute; }
        }
        public PropertyInfo Property
        {
            get { return m_pInfo; }
        }
        public PackageDataTypes PackageDataType
        {
            get { return m_PackageDataType; }
        }
        public string StringValue
        {
            get { return m_strValue; }
        }
        public uint UIntValue
        {
            get { return m_IntValue; }
        }
        public override string ToString()
        {
            return null == m_pInfo ? "[非法节点]" :
                m_pInfo.Name + "(" +
                m_PackageDataType + ")=" +
                (m_PackageDataType != PackageDataTypes.String ? "" + m_IntValue : "" + m_strValue);

        }
        #endregion

        #region Function
        public bool IsProperty(String str)
        {
            return null != m_pInfo && null != str &&
                m_pInfo.Name.Trim().ToLower().Equals(str.Trim().ToLower());
        }

        public void ReBuildPackage()
        {
            if (m_pInfo.PropertyType != typeof(string))
            {
                Type type = m_pInfo.PropertyType;
                UInt32 nValue = Convert.ToUInt32(m_pInfo.GetValue(m_command, null));
                m_IntValue = nValue;
                m_PackageDataType = PackageDataTypes.Uint;
                m_nLength = 4;

                if ((type == typeof(Enum) || type.BaseType == typeof(Enum)) &&
                    type.GetFields() != null && type.GetFields().Length > 0)
                {
                    if (type.GetFields()[0].FieldType == typeof(byte))
                    {
                        m_nLength = 1;
                        m_PackageDataType = PackageDataTypes.Byte;
                    }
                }
                else if (type == typeof(byte))
                {
                    m_nLength = 1;
                    m_PackageDataType = PackageDataTypes.Byte;
                }
            }
            else
            {
                string sValue = m_pInfo.GetValue(m_command, null) as string;
                if (null == sValue)
                {
                    sValue = "";
                }
                m_PackageDataType = PackageDataTypes.String;
                m_strValue = sValue;
                m_nLength = (uint)m_attribute.Length;
                m_nLength = (m_nLength <= 0 || m_nLength >1024? (uint)System.Text.Encoding.Default.GetByteCount(sValue) : m_nLength);
                System.Console.WriteLine(m_pInfo.Name + "-["+sValue+"]--" + m_nLength);
            }
        }
        #endregion
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -