学习C#中调用COM,后期绑定(以及对WinHttp COM对象的C#封装)

简介:
学习C#中调用COM,后期绑定全部代码  开始学习C#了,没打算从语法一点一点的看起!所以上来就直接开始代码了!同时也和Delphi之间的某些操作进行比较比较!于是想到了一个朋友用 Delphi写的通过ip138查询手机号码的例子,他用的是IdHttp来进行提交操作的。我在之前写飞信的控件的时候,用的http是Windows 自己内部的COM库:WinHttp.WinHttpRequest.5.1,这个玩意用起来还是相当方便的,于是就乘着学习,借机来在C#中动态创建这 个COM对象。要知道在Delphi中创建和调用一个COM对象,那是相当方便的。直接一个CreateOleObject就能创建一个COM对象了,然 后COM对象中的方法就能够直接通过这个东西直接调用。那么在C#中是否依然如此简洁?不晓得,是因为我刚接触还是咋地,折磨了半天,实在没摸索到简单的 方法来创建COM对象(当然,那种通过引用添加组件库到C#环境中去的方法不算,因为这个方法有一定的弊端,那便是当用户换了环境,重新来编译本程序的时 候,由于新的环境中可能并没有引用这个库,所以直接编译会出错,于是咱们又要重新添加引用一次本库!),唯一的办法就是后期绑定COM对象(至于是否有简 单方法,我不知道的,因为才学1天)。查阅了相关的一些资料之后得知,首先要通过COM对象名称获得这个对象在系统全局的一个ProgId,然后通过这个 ProgId获得这个对象的类型:

System.Type _ObjType = System.Type.GetTypeFromProgID(ComName);

然后通过这个类型来创建一个对象接口

object ComInstance= System.Activator.CreateInstance(_ObjType);       

之后,对该对象方法的调用都要通过type的InvokeMember方法来进行比如:

return ComType.InvokeMember(MethodName,System.Reflection.BindingFlags.InvokeMethod,null,ComInstance,args);

哎!写起来的代码量还真是有点大呢!于是,产生了一个想法,将这个COM对象后期绑定的玩意封装起来做一个公共的,能省多少代码就省多少代码,尽量的减少代码量:

复制代码
代码
/*
 * Created by SharpDevelop.
 * User: Administrator
 * Date: 2009-12-2
 * Time: 23:26
 * 
 * To change this template use Tools | Options | Coding | Edit Standard Headers.
 
*/

using  System;

namespace  DxComOperate
{
    
///   <summary>
    
///  COM对象的后期绑定调用类库    
    
///   </summary>
     public   class  DxComObject
    {
        
private  System.Type _ObjType;
        
private   object  ComInstance;
        
/* public DxComObject()
        {
            throw new
        }
*/
        
public  DxComObject( string  ComName)
        {
            
// 根据COM对象的名称创建COM对象
            _ObjType  =  System.Type.GetTypeFromProgID(ComName);
            
if (_ObjType == null )
                
throw   new  Exception( " 指定的COM对象名称无效 " );
            ComInstance 
=  System.Activator.CreateInstance(_ObjType);            
        }
        
public  System.Type ComType
        {
            
get { return  _ObjType;}
        }
        
// 执行的函数
         public   object  DoMethod( string  MethodName, object [] args)
        {
            
return  ComType.InvokeMember(MethodName,System.Reflection.BindingFlags.InvokeMethod, null ,ComInstance,args);
        }
        
// 获得属性与设置属性
         public   object   this [ string  propName]
        {
            
get
            {
                
return  _ObjType.InvokeMember( propName, System.Reflection.BindingFlags.GetProperty,  null , ComInstance,  null  );
            }
            
set
            {                                
                _ObjType.InvokeMember( propName, System.Reflection.BindingFlags.SetProperty, 
null , ComInstance,  new   object [] {value} );
            }
        }            
    }
    
    
    
///   <summary>
    
///  WinHttp对象库
    
///   </summary>
     public   class  DxWinHttp
    {
        
private  DxComObject HttpObj;
        
private   string  _ContentType;
        
public  DxWinHttp()
        {
            
// 构建WinHttp对象
            HttpObj  =   new  DxComObject( " WinHttp.WinHttpRequest.5.1 " );
        }
        
// 设置Content-Type属性
         public   string  ContentType
        {
            
get { return  _ContentType;}
            
set
            {
                
if  (_ContentType.CompareTo(value) != 0 )
                {
                    _ContentType 
=  value;
                    
object [] args  =   new   object [ 2 ];
                    args[
0 =   " Content-Type " ;args[ 1 =  value;
                    HttpObj.DoMethod(
" SetRequestHeader " ,args);
                }                                
            }
        }
        
        
    }
}
复制代码

 

代码写的也不复杂,就这么几句,但是我想应该看起来更加明亮化了。

比如,我现在通过这个封装操作库来通过ip138查询手机号码的归属地


 

复制代码
代码
void  Button1Click( object  sender, EventArgs e)
        {
            
if  (textBox1.Text.Length  ==   0 )
            {
                MessageBox.Show(
" 请输入一个手机号 " );
                
return ;
            }
            DxComObject WinHttp 
=   new  DxComObject( " WinHttp.WinHttpRequest.5.1 " );
            
string  PostData  =   " http://www.ip138.com:8080/search.asp " ;    
            
object [] args  =   new   object [ 3 ]; // 指定参数
            args[ 0 =   " GET " ;args[ 1 =  PostData;args[ 2 =   false ;
            WinHttp.DoMethod(
" Open " ,args); // 执行Open方法            
            PostData  =   " action=mobile&mobile= " + this .textBox1.Text;    
            
            args 
=   new   object [ 2 ];
            args[
0 =   " Content-Length " ;args[ 1 =  PostData.Length;
            WinHttp.DoMethod(
" SetRequestHeader " ,args);
            
            args 
=   new   object [ 2 ];
            args[
0 =   " Content-Type " ;args[ 1 =   " application/x-www-form-urlencoded " ;
            WinHttp.DoMethod(
" SetRequestHeader " ,args);
            
            args 
=   new   object [ 2 ];
            args[
0 =   " User-Agent " ;args[ 1 =   " Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1) " ;
            WinHttp.DoMethod(
" SetRequestHeader " ,args);
            
            args 
=   new   object [ 1 ];
            args[
0 =  PostData;
            WinHttp.DoMethod(
" Send " ,args);
            
            DxComObject AdoStream 
=   new  DxComObject( " Adodb.Stream " );
            AdoStream[
" Type " =   1 ;
            AdoStream[
" Mode " =   3 ;
            
            args 
=   new   object []{};
            AdoStream.DoMethod(
" Open " ,args);
            
            args 
=   new   object [ 1 ]{WinHttp[ " ResponseBody " ]};
            AdoStream.DoMethod(
" Write " ,args);
            AdoStream[
" Position " =   0 ;
            AdoStream[
" Type " =   2 ;
            AdoStream[
" Charset " =   " GB2312 " ;
            
string  Result  =  AdoStream[ " ReadText " ].ToString();
            
            Info.mobileNo 
=  GetReturnValue(DxValueType.DxPhone,Result);
            Info.City 
=  GetReturnValue(DxValueType.DxPhoneCity,Result);            
            Info.CardType 
=  GetReturnValue(DxValueType.DxCardType,Result);
            Info.CityCode 
=  GetReturnValue(DxValueType.DxCityCode,Result);
            
this .propertyGrid1.Refresh();                        
        }
复制代码

//可见现在这个代码就已经很明显很明白了,但是写的代码量还是有些大,如果反复使用的话,就不爽了还是要写比较多的代码,于是开始着手对WinHttp这个COM对象进行封装


 

复制代码
代码
///   <summary>
    
///  WinHttp对象库
    
///   </summary>
     public   class  DxWinHttp
    {
        
private  DxComObject HttpObj;
        
private   string  _ContentType;
        
private   int  _ContentLength;
        
private   bool  _Active;
        
private  System.Collections.ArrayList PostDataList; // 提交的数据字段
         public  DxWinHttp()
        {
            
// 构建WinHttp对象
            HttpObj  =   new  DxComObject( " WinHttp.WinHttpRequest.5.1 " );            
            _ContentType 
=   " application/x-www-form-urlencoded " ;
            _ContentLength 
=   0 ;    
            PostDataList 
=   new  System.Collections.ArrayList();
        }
        
        
// 提交参数信息的个数
         public   int  PostDataCount
        {
            
get { return  PostDataList.Count;}
        }
        
// 设置Content-Type属性
         public   string  ContentType
        {
            
get { return  _ContentType;}
            
set
            {
                
if ( ! _Active) _ContentType  =  value;
                
else   if (_ContentType.CompareTo(value) != 0 )
                {
                    _ContentType 
=  value;
                    SetRequestHeader(
" Content-Type " ,_ContentType);
                }
            }
        }
        
        
// 对象是否是打开状态
         public   bool  Active
        {
            
get { return  _Active;}
        }
        
        
// 设置Send数据的长度 
         public   int  ContentLength
        {
            
get { return  _ContentLength;}
            
set
            {
                
if ( ! _Active) _ContentLength  =  value;
                
else   if (_ContentLength  !=  value)
                {
                    _ContentLength 
=  value;
                    HttpObj.DoMethod(
" SetRequestHeader " , new   object [ 2 ]{ " Content-Length " ,value});
                }
            }
        }
        
        
// 执行之后返回的结果
         public   string  ResponseBody
        {
            
get
            {
                
if (_Active)
                {
                    DxComObject AdoStream 
=   new  DxComObject( " Adodb.Stream " );
                    AdoStream[
" Type " =   1 ;
                    AdoStream[
" Mode " =   3 ;
                    AdoStream.DoMethod(
" Open " , new   object []{});
                    AdoStream.DoMethod(
" Write " , new   object [ 1 ]{HttpObj[ " ResponseBody " ]});
                    AdoStream[
" Position " =   0 ;
                    AdoStream[
" Type " =   2 ;
                    AdoStream[
" Charset " =   " GB2312 " ;
                    
return  AdoStream[ " ReadText " ].ToString();
                }
                
else   return   "" ;
            }
        }
        
        
// 设定请求头
         public   string  SetRequestHeader( string  Header, object  Value)
        {        
            
object  obj;
            obj 
=  HttpObj.DoMethod( " SetRequestHeader " , new   object []{Header,Value});
            
if (obj  !=   null return  obj.ToString();
            
else   return   " True " ;
        }
        
        
// 打开URL执行OpenMethod方法,Async指定是否采用异步方式调用,异步方式不会阻塞
         public   string  Open( string  OpenMethod, string  URL, bool  Async)
        {
            
object  obj;
            obj 
=  HttpObj.DoMethod( " Open " , new   object []{OpenMethod,URL,Async});            
            
if  (obj  !=   null
            {
                _Active 
=   false ;
                
return  obj.ToString();
            }
            
else
            {                
                SetRequestHeader(
" Content-Type " ,_ContentType);
                SetRequestHeader(
" User-Agent " , " Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1) " );
                
if  (_ContentLength  !=   0 ) SetRequestHeader( " Content-Length " ,_ContentLength);                
                _Active 
=   true ;
                
return   " True " ;
            }
        }            
        
        
// 发送数据
         public   string  Send( string  body)
        {
            
if ( ! _Active)  return   " False " ;
            
object  obj;
            obj 
=  HttpObj.DoMethod( " Send " , new   object [ 1 ]{body});
            
if (obj  !=   null return  obj.ToString();
            
else   return   " True " ;
        }
        
        
public   void  ClearPostData()
        {
            
this .PostDataList.Clear();
        }
        
// 增加提交数据信息
         public   void  AddPostField( string  FieldName, object  Value)
        {
            
this .PostDataList.Add(FieldName + " = " + Value.ToString());
        }
        
        
// 通过参数指定提交
         public   string  DxPost()
        {
            
if ( ! _Active)
            {
                
return   " False " ;
            }
            
string  st = "" ;
            
for ( int  i = 0 ;i < this .PostDataList.Count;i ++ )
            {
                
if (st  !=   "" ) st  =  st  +   " & " +  PostDataList[i].ToString();
                
else  st  =  PostDataList[i].ToString();
            }
            
this .ContentLength  =  st.Length;
            
return  Send(st);
        }
        
        
// 设置等待超时等
         public   string  SetTimeouts( long  ResolveTimeout, long  ConnectTimeout, long  SendTimeout, long  ReceiveTimeout)
        {
            
object  obj;
            obj 
=  HttpObj.DoMethod( " SetTimeouts " , new   object [ 4 ]{ResolveTimeout,ConnectTimeout,SendTimeout,ReceiveTimeout});
            
if (obj  !=   null return  obj.ToString();
            
else   return   " True " ;
        }
        
// 等待数据提交完成
         public   string  WaitForResponse( object  Timeout, out   bool  Succeeded)
        {
            
if ( ! _Active) {Succeeded  =   false ; return   "" ;}
            
object  obj;
            
bool  succ;
            succ 
=   false ;
            System.Reflection.ParameterModifier[] ParamesM;
            ParamesM 
=   new  System.Reflection.ParameterModifier[ 1 ];
            ParamesM[
0 =   new  System.Reflection.ParameterModifier ( 2 );  //  初始化为接口参数的个数
            ParamesM[ 0 ][ 1 =   true //  设置第二个参数为返回参数
            
            
// ParamesM[1] = true;
             object [] ParamArray  =   new   object [ 2 ]{Timeout,succ};
            obj 
=  HttpObj.DoMethod( " WaitForResponse " ,ParamArray,ParamesM);
            System.Windows.Forms.MessageBox.Show(ParamArray[
1 ].ToString());
            Succeeded
= bool .Parse(ParamArray[ 1 ].ToString());
            
// Succeeded = bool.Parse(ParamArray[1].ToString);
             if (obj  !=   null )    { return  obj.ToString();}
            
else   return   " True " ;
        }
        
        
public   string  GetResponseHeader( string  Header, ref   string  Value)
        {
            
if ( ! _Active) {Value = "" ; return   "" ;}
            
object  obj;
            
/* string str;
            str = "";
            System.Reflection.ParameterModifier[] Parames;
            Parames = new System.Reflection.ParameterModifier[1];
            Parames[0] = new System.Reflection.ParameterModifier (2); // 初始化为接口参数的个数
            Parames[0][1] = true; 
*///  设置第二个参数为返回参数
            obj  =  HttpObj.DoMethod( " GetResponseHeader " , new   object [ 2 ]{Header,Value});
            
// Value = str;
             if (obj  !=   null ) { return  obj.ToString();}
            
else   return   " True " ;
        }
    }
复制代码

封装完成,此时再看,用本库来查询手机归属地的代码:


 

复制代码
代码
void  Button2Click( object  sender, EventArgs e)
        {
            
if  (textBox1.Text.Length  ==   0 )
            {
                MessageBox.Show(
" 请输入一个手机号 " );
                
return ;
            }            
            WinHttp.Open(
" GET " , " http://www.ip138.com:8080/search.asp " , false );                
            WinHttp.ClearPostData();
            WinHttp.AddPostField(
" action " , " mobile " );
            WinHttp.AddPostField(
" mobile " , this .textBox1.Text);
            WinHttp.DxPost();
            
string  Result  =  WinHttp.ResponseBody;
            Info.mobileNo 
=  GetReturnValue(DxValueType.DxPhone,Result);
            Info.City 
=  GetReturnValue(DxValueType.DxPhoneCity,Result);            
            Info.CardType 
=  GetReturnValue(DxValueType.DxCardType,Result);
            Info.CityCode 
=  GetReturnValue(DxValueType.DxCityCode,Result);
            
this .propertyGrid1.Refresh();    
        }
复制代码

现在的代码已经减少到了很多了!

 

下面给出完整的COM封装类库代码:


 

复制代码
代码
/*
 * Created by SharpDevelop.
 * 作者: 不得闲
 * Date: 2009-12-2
 * Time: 23:26
 * 
 * To change this template use Tools | Options | Coding | Edit Standard Headers.
 
*/
using  System;
namespace  DxComOperate
{
    
///   <summary>
    
///  COM对象的后期绑定调用类库    
    
///   </summary>
     public   class  DxComObject
    {
        
private  System.Type _ObjType;
        
private   object  ComInstance;
        
/* public DxComObject()
        {
            throw new
        }
*/
        
public  DxComObject( string  ComName)
        {
            
// 根据COM对象的名称创建COM对象
            _ObjType  =  System.Type.GetTypeFromProgID(ComName);
            
if (_ObjType == null )
                
throw   new  Exception( " 指定的COM对象名称无效 " );
            ComInstance 
=  System.Activator.CreateInstance(_ObjType);            
        }
        
public  System.Type ComType
        {
            
get { return  _ObjType;}
        }
        
// 执行的函数
         public   object  DoMethod( string  MethodName, object [] args)
        {
            
return  ComType.InvokeMember(MethodName,System.Reflection.BindingFlags.InvokeMethod, null ,ComInstance,args);
        }
        
        
public   object  DoMethod( string  MethodName, object [] args,System.Reflection.ParameterModifier[] ParamMods)
        {            
            
return  ComType.InvokeMember(MethodName,System.Reflection.BindingFlags.InvokeMethod, null ,ComInstance,args,ParamMods, null , null );
        }
        
// 获得属性与设置属性
         public   object   this [ string  propName]
        {
            
get
            {
                
return  _ObjType.InvokeMember( propName, System.Reflection.BindingFlags.GetProperty,  null , ComInstance,  null  );
            }
            
set
            {                                
                _ObjType.InvokeMember( propName, System.Reflection.BindingFlags.SetProperty, 
null , ComInstance,  new   object [] {value} );
            }
        }            
    }
    
    
    
///   <summary>
    
///  WinHttp对象库
    
///   </summary>
     public   class  DxWinHttp
    {
        
private  DxComObject HttpObj;
        
private   string  _ContentType;
        
private   int  _ContentLength;
        
private   bool  _Active;
        
private  System.Collections.ArrayList PostDataList; // 提交的数据字段
         public  DxWinHttp()
        {
            
// 构建WinHttp对象
            HttpObj  =   new  DxComObject( " WinHttp.WinHttpRequest.5.1 " );            
            _ContentType 
=   " application/x-www-form-urlencoded " ;
            _ContentLength 
=   0 ;    
            PostDataList 
=   new  System.Collections.ArrayList();
        }
        
        
// 提交参数信息的个数
         public   int  PostDataCount
        {
            
get { return  PostDataList.Count;}
        }
        
// 设置Content-Type属性
         public   string  ContentType
        {
            
get { return  _ContentType;}
            
set
            {
                
if ( ! _Active) _ContentType  =  value;
                
else   if (_ContentType.CompareTo(value) != 0 )
                {
                    _ContentType 
=  value;
                    SetRequestHeader(
" Content-Type " ,_ContentType);
                }
            }
        }
        
        
// 对象是否是打开状态
         public   bool  Active
        {
            
get { return  _Active;}
        }
        
        
// 设置Send数据的长度 
         public   int  ContentLength
        {
            
get { return  _ContentLength;}
            
set
            {
                
if ( ! _Active) _ContentLength  =  value;
                
else   if (_ContentLength  !=  value)
                {
                    _ContentLength 
=  value;
                    HttpObj.DoMethod(
" SetRequestHeader " , new   object [ 2 ]{ " Content-Length " ,value});
                }
            }
        }
        
        
// 执行之后返回的结果
         public   string  ResponseBody
        {
            
get
            {
                
if (_Active)
                {
                    DxComObject AdoStream 
=   new  DxComObject( " Adodb.Stream " );
                    AdoStream[
" Type " =   1 ;
                    AdoStream[
" Mode " =   3 ;
                    AdoStream.DoMethod(
" Open " , new   object []{});
                    AdoStream.DoMethod(
" Write " , new   object [ 1 ]{HttpObj[ " ResponseBody " ]});
                    AdoStream[
" Position " =   0 ;
                    AdoStream[
" Type " =   2 ;
                    AdoStream[
" Charset " =   " GB2312 " ;
                    
return  AdoStream[ " ReadText " ].ToString();
                }
                
else   return   "" ;
            }
        }
        
        
// 设定请求头
         public   string  SetRequestHeader( string  Header, object  Value)
        {        
            
object  obj;
            obj 
=  HttpObj.DoMethod( " SetRequestHeader " , new   object []{Header,Value});
            
if (obj  !=   null return  obj.ToString();
            
else   return   " True " ;
        }
        
        
// 打开URL执行OpenMethod方法,Async指定是否采用异步方式调用,异步方式不会阻塞
         public   string  Open( string  OpenMethod, string  URL, bool  Async)
        {
            
object  obj;
            obj 
=  HttpObj.DoMethod( " Open " , new   object []{OpenMethod,URL,Async});            
            
if  (obj  !=   null
            {
                _Active 
=   false ;
                
return  obj.ToString();
            }
            
else
            {                
                SetRequestHeader(
" Content-Type " ,_ContentType);
                SetRequestHeader(
" User-Agent " , " Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1) " );
                
if  (_ContentLength  !=   0 ) SetRequestHeader( " Content-Length " ,_ContentLength);                
                _Active 
=   true ;
                
return   " True " ;
            }
        }            
        
        
// 发送数据
         public   string  Send( string  body)
        {
            
if ( ! _Active)  return   " False " ;
            
object  obj;
            obj 
=  HttpObj.DoMethod( " Send " , new   object [ 1 ]{body});
            
if (obj  !=   null return  obj.ToString();
            
else   return   " True " ;
        }
        
        
public   void  ClearPostData()
        {
            
this .PostDataList.Clear();
        }
        
// 增加提交数据信息
         public   void  AddPostField( string  FieldName, object  Value)
        {
            
this .PostDataList.Add(FieldName + " = " + Value.ToString());
        }
        
        
// 通过参数指定提交
         public   string  DxPost()
        {
            
if ( ! _Active)
            {
                
return   " False " ;
            }
            
string  st = "" ;
            
for ( int  i = 0 ;i < this .PostDataList.Count;i ++ )
            {
                
if (st  !=   "" ) st  =  st  +   " & " +  PostDataList[i].ToString();
                
else  st  =  PostDataList[i].ToString();
            }
            
this .ContentLength  =  st.Length;
            
return  Send(st);
        }
        
        
// 设置等待超时等
         public   string  SetTimeouts( long  ResolveTimeout, long  ConnectTimeout, long  SendTimeout, long  ReceiveTimeout)
        {
            
object  obj;
            obj 
=  HttpObj.DoMethod( " SetTimeouts " , new   object [ 4 ]{ResolveTimeout,ConnectTimeout,SendTimeout,ReceiveTimeout});
            
if (obj  !=   null return  obj.ToString();
            
else   return   " True " ;
        }
        
// 等待数据提交完成
         public   string  WaitForResponse( object  Timeout, out   bool  Succeeded)
        {
            
if ( ! _Active) {Succeeded  =   false ; return   "" ;}
            
object  obj;
            
bool  succ;
            succ 
=   false ;
            System.Reflection.ParameterModifier[] ParamesM;
            ParamesM 
=   new  System.Reflection.ParameterModifier[ 1 ];
            ParamesM[
0 =   new  System.Reflection.ParameterModifier ( 2 );  //  初始化为接口参数的个数
            ParamesM[ 0 ][ 1 =   true //  设置第二个参数为返回参数
            
            
// ParamesM[1] = true;
             object [] ParamArray  =   new   object [ 2 ]{Timeout,succ};
            obj 
=  HttpObj.DoMethod( " WaitForResponse " ,ParamArray,ParamesM);
            System.Windows.Forms.MessageBox.Show(ParamArray[
1 ].ToString());
            Succeeded
= bool .Parse(ParamArray[ 1 ].ToString());
            
// Succeeded = bool.Parse(ParamArray[1].ToString);
             if (obj  !=   null )    { return  obj.ToString();}
            
else   return   " True " ;
        }
        
        
public   string  GetResponseHeader( string  Header, ref   string  Value)
        {
            
if ( ! _Active) {Value = "" ; return   "" ;}
            
object  obj;
            
/* string str;
            str = "";
            System.Reflection.ParameterModifier[] Parames;
            Parames = new System.Reflection.ParameterModifier[1];
            Parames[0] = new System.Reflection.ParameterModifier (2); // 初始化为接口参数的个数
            Parames[0][1] = true; 
*///  设置第二个参数为返回参数
            obj  =  HttpObj.DoMethod( " GetResponseHeader " , new   object [ 2 ]{Header,Value});
            
// Value = str;
             if (obj  !=   null ) { return  obj.ToString();}
            
else   return   " True " ;
        }
    }
}
复制代码

这个对WinHttp的封装,还并不完全,只是封装了一部分的方法和属性,剩下的,如果又需要的人,可自行封装!

Ip138手机号码查询


本文转自 不得闲 博客园博客,原文链接: http://www.cnblogs.com/DxSoft/archive/2010/01/02/1637669.html ,如需转载请自行联系原作者

相关文章
|
10月前
|
Java 调度 C#
C#学习系列相关之多线程(一)----常用多线程方法总结
C#学习系列相关之多线程(一)----常用多线程方法总结
109 0
|
4月前
|
Java 物联网 C#
C#/.NET/.NET Core学习路线集合,学习不迷路!
C#/.NET/.NET Core学习路线集合,学习不迷路!
245 0
|
3月前
|
程序员 C# 数据库
C# 比较对象新思路,利用反射技术打造更灵活的比较工具
中途接手的项目,碰到需要在更新对象信息时比较并记录差异的需求,最变态的还有附加要求,怎么办?有没有既能满足需求又能对项目影响最小的方法呢?分享这个我封装的方法,一个利用反射技术打造的更灵活的比较工具
|
5月前
|
编译器 C#
C#多态概述:通过继承实现的不同对象调用相同的方法,表现出不同的行为
C#多态概述:通过继承实现的不同对象调用相同的方法,表现出不同的行为
158 65
|
4月前
|
JSON 程序员 C#
使用 C# 比较两个对象是否相等的7个方法总结
比较对象是编程中的一项基本技能,在实际业务中经常碰到,比如在ERP系统中,企业的信息非常重要,每一次更新,都需要比较记录更新前后企业的信息,直接比较通常只能告诉我们它们是否指向同一个内存地址,那我们应该怎么办呢?分享 7 个方法给你!
104 2
|
10月前
|
C#
C#的基本语法结构学习
【5月更文挑战第17天】C#基础语法包括变量(如`int x = 10`)、常量(`const int MAX_VALUE = 100`)、运算符(如算术和比较运算符)、控制语句(if、for、while等)和函数声明(`int Add(int x, int y) { return x + y; }`)。这些构成C#程序的基本元素。
99 0
|
6月前
|
安全 C#
C# 面向对象编程的三大支柱:封装、继承与多态
【9月更文挑战第17天】在C#中,面向对象编程的三大支柱——封装、继承与多态,对于编写安全、可维护、可复用的代码至关重要。封装通过访问修饰符和属性保护数据;继承允许子类继承父类的属性和方法,实现代码复用和多态;多态则提高了代码的灵活性和通用性。掌握这三大概念能显著提升C#编程能力,优化开发效率和代码质量。
|
5月前
|
开发框架 缓存 算法
开源且实用的C#/.NET编程技巧练习宝库(学习,工作,实践干货)
开源且实用的C#/.NET编程技巧练习宝库(学习,工作,实践干货)
547 0
|
6月前
|
Linux C# 开发者
Uno Platform 驱动的跨平台应用开发:从零开始的全方位资源指南与定制化学习路径规划,助您轻松上手并精通 C# 与 XAML 编程技巧,打造高效多端一致用户体验的移动与桌面应用程序
【9月更文挑战第8天】Uno Platform 的社区资源与学习路径推荐旨在为初学者和开发者提供全面指南,涵盖官方文档、GitHub 仓库及社区支持,助您掌握使用 C# 和 XAML 创建跨平台原生 UI 的技能。从官网入门教程到进阶技巧,再到活跃社区如 Discord,本指南带领您逐步深入了解 Uno Platform,并提供实用示例代码,帮助您在 Windows、iOS、Android、macOS、Linux 和 WebAssembly 等平台上高效开发。建议先熟悉 C# 和 XAML 基础,然后实践官方教程,研究 GitHub 示例项目,并积极参与社区讨论,不断提升技能。
192 2
|
6月前
|
C# 数据安全/隐私保护
C# 一分钟浅谈:类与对象的概念理解
【9月更文挑战第2天】本文从零开始详细介绍了C#中的类与对象概念。类作为一种自定义数据类型,定义了对象的属性和方法;对象则是类的实例,拥有独立的状态。通过具体代码示例,如定义 `Person` 类及其实例化过程,帮助读者更好地理解和应用这两个核心概念。此外,还总结了常见的问题及解决方法,为编写高质量的面向对象程序奠定基础。
72 2