using System;
namespace Oceansoft.LOGERP.MessageClient.MessageHeadAffirm
{
/// < summary >
/// 报文头的形成
/// 作者:贾海天
/// </ summary >
public class MessageHead
{
// 相关数组中存储项目的数组
protected object [] _items;
// 数组中的项目数
protected int _ count = 0 ;
public int Length
{
get
{
return _ count ;
}
}
public object this [ string key ]
{
get
{
return KeyToObject( key );
}
set
{
AddToArray( key ,value);
}
}
protected bool KeyExists(string key )
{
for ( int n = 0 ;n < _ count ;n ++ )
{
KeyItemPair pair = (KeyItemPair)_items [ n ] ;
if (pair. key == key )
{
return true;
}
}
return false;
}
protected void AddToArray(string key ,object item)
{
if (!KeyExists( key ))
{
_items [ _count ] = new KeyItemPair( key ,item);
_ count ++ ;
}
}
protected object KeyToObject(string key )
{
for ( int n = 0 ;n < _ count ; ++ n)
{
KeyItemPair pair = (KeyItemPair)_items [ n ] ;
if (pair. key == key )
{
return pair.item;
}
}
return null ;
}
public MessageHead( int MessageHead_Length)
{
//
// TODO: 在此处添加构造函数逻辑
//
_items = new object [ MessageHead_Length ] ;
}
protected struct KeyItemPair
{
public object item;
public string key ;
public KeyItemPair(string k,object obj)
{
key = k;
item = obj;
}
}
}
}
使用方法(方法不是很完整,可以继续扩展):
namespace Oceansoft.LOGERP.MessageClient.MessageHeadAffirm
{
/// < summary >
/// 报文头的形成
/// 作者:贾海天
/// </ summary >
public class MessageHead
{
// 相关数组中存储项目的数组
protected object [] _items;
// 数组中的项目数
protected int _ count = 0 ;
public int Length
{
get
{
return _ count ;
}
}
public object this [ string key ]
{
get
{
return KeyToObject( key );
}
set
{
AddToArray( key ,value);
}
}
protected bool KeyExists(string key )
{
for ( int n = 0 ;n < _ count ;n ++ )
{
KeyItemPair pair = (KeyItemPair)_items [ n ] ;
if (pair. key == key )
{
return true;
}
}
return false;
}
protected void AddToArray(string key ,object item)
{
if (!KeyExists( key ))
{
_items [ _count ] = new KeyItemPair( key ,item);
_ count ++ ;
}
}
protected object KeyToObject(string key )
{
for ( int n = 0 ;n < _ count ; ++ n)
{
KeyItemPair pair = (KeyItemPair)_items [ n ] ;
if (pair. key == key )
{
return pair.item;
}
}
return null ;
}
public MessageHead( int MessageHead_Length)
{
//
// TODO: 在此处添加构造函数逻辑
//
_items = new object [ MessageHead_Length ] ;
}
protected struct KeyItemPair
{
public object item;
public string key ;
public KeyItemPair(string k,object obj)
{
key = k;
item = obj;
}
}
}
}
MessageHead MH
=
new MessageHead(
3
);
MH [ "Confirm_NO" ] = "aaa";
MH [ "IO_Seq" ] = "bbb";
MH [ "Gross_Pack_NO" ] = "ccc";
MH [ "Confirm_NO" ] = "aaa";
MH [ "IO_Seq" ] = "bbb";
MH [ "Gross_Pack_NO" ] = "ccc";
注意:
Struct 的实现是在栈里面实现的
Class 实现是在托管堆里面实现的
Class里面的方法是在托管堆里面的方法表里面保存的.
此方法也可以用 Hashtable 来实现,具体实现将在以后进行研究