C#制作ActiveX控件中如何调用海康SDK的问题解决

  这个事情就是一个坑,耽误了两周时间,之前并没有做过activex这玩意,现在客户需求如此,只能说是在网上看着教程做了。

  事情是这样的,有一台海康威视的摄像头,客户需要一个ActiveX控件嵌入到网页中,通过点击按钮开始录制和结束录制来进行视频的录制和保存,关于海康摄像头的二次开发在此就不多说了,可以参考SDK中的说明。

  直接上流程:

  1.开发环境:

    VS2010,这个打包方便,之前用VS2013打包的,总是调用不了,不知道原因是什么;SDK是32位的,用64位的在Winform中可以正常使用,在网页中使用控件时会报错。

  2.新建项目:

    新建一个类库项目,如下:

 

C#制作ActiveX控件中如何调用海康SDK的问题解决

    右键点击项目,添加“用户控件”,如下:

C#制作ActiveX控件中如何调用海康SDK的问题解决

    界面拖控件,如下:

C#制作ActiveX控件中如何调用海康SDK的问题解决

    控件代码如下,其中Guid是“工具”->“创建GUID”自动生成的,#region->#endregion折叠部分是实现的IObjectSafety接口

using System;namespace VideoHelper{    [System.Security.SecuritySafeCritical]    public class Videos    {        private bool m_initSDK = false;        ///         /// 正在录制        ///         private bool m_Record = false;        private uint LastErr = 0;        private Int32 m_RealHandle = -1;        private Int32 m_lUserID = -1;        public IntPtr handle { get; set; }        public bool Initialize(string ip = "192.168.1.64", int port = 8000, string username = "admin", string password = "8910jqk#")        {            try            {                m_initSDK = CHCNetSDK.NET_DVR_Init();                if (m_initSDK)                {                    CHCNetSDK.NET_DVR_SetLogToFile(3, "C:SdkLog", true);                    //设备参数结构体                    CHCNetSDK.NET_DVR_DEVICEINFO_V30 DeviceInfo = new CHCNetSDK.NET_DVR_DEVICEINFO_V30();                    //注册设备                    m_lUserID = CHCNetSDK.NET_DVR_Login_V30(ip, port, username, password, ref DeviceInfo);                    return m_lUserID >= 0;                }                return false;            }            catch (Exception ex)            {                System.Windows.Forms.MessageBox.Show("Initialize:" + ex.Message);                return false;            }        }        public bool Start(IntPtr handle, string filename)        {            try            {                CHCNetSDK.NET_DVR_PREVIEWINFO lpPreviewInfo = new CHCNetSDK.NET_DVR_PREVIEWINFO();                lpPreviewInfo.lChannel = 1;                lpPreviewInfo.dwLinkMode = 0;                lpPreviewInfo.dwStreamType = 0;                lpPreviewInfo.bBlocked = true;                lpPreviewInfo.dwDisplayBufNum = 15;                lpPreviewInfo.hPlayWnd = handle;                IntPtr pUser = IntPtr.Zero;//new IntPtr();                         //获取实时视频流                m_RealHandle = CHCNetSDK.NET_DVR_RealPlay_V40(m_lUserID, ref lpPreviewInfo, null, pUser);                if (m_Record == false)                {                    CHCNetSDK.NET_DVR_MakeKeyFrame(m_lUserID, 1);                    if (!CHCNetSDK.NET_DVR_SaveRealData(m_RealHandle, filename))                    {                        LastErr = CHCNetSDK.NET_DVR_GetLastError();                        return false;                    }                    else                    {                        m_Record = true;                        return true;                    }                }                else                {                    return false;                }            }            catch            {                return false;            }        }        public bool End()        {            if (m_Record)            {                if (!CHCNetSDK.NET_DVR_StopSaveRealData(m_RealHandle))                {                    LastErr = CHCNetSDK.NET_DVR_GetLastError();                    return false;                }                m_Record = false;                m_RealHandle = -1;                return true;            }            else            {                return false;            }        }        public void Dispose()        {            try            {                if (m_lUserID >= 0)                {                    CHCNetSDK.NET_DVR_Logout_V30(m_lUserID);                    m_lUserID = -1;                }                if (m_RealHandle >= 0)                {                    CHCNetSDK.NET_DVR_StopRealPlay(m_RealHandle);                    m_RealHandle = -1;                }                CHCNetSDK.NET_DVR_Cleanup();            }            catch            { }        }    }}录制视频操作类

录制视频操作类

using System;using System.Runtime.InteropServices;namespace VideoHelper{    [ComImport, GuidAttribute("CB5BDC81-93C1-11CF-8F20-00805F2CD064")]    [InterfaceTypeAttribute(ComInterfaceType.InterfaceIsIUnknown)]    public interface IObjectSafety    {        [PreserveSig]        int GetInterfaceSafetyOptions(ref Guid riid, [MarshalAs(UnmanagedType.U4)] ref int pdwSupportedOptions, [MarshalAs(UnmanagedType.U4)] ref int pdwEnabledOptions);        [PreserveSig()]        int SetInterfaceSafetyOptions(ref Guid riid, [MarshalAs(UnmanagedType.U4)] int dwOptionSetMask, [MarshalAs(UnmanagedType.U4)] int dwEnabledOptions);    }}接口代码
using System;using System.Windows.Forms;using System.IO;using System.Runtime.InteropServices;namespace VideoHelper{    [System.Security.SecuritySafeCritical]    [Guid("79629620-3C0C-4D47-B93B-2D36AEF8EF31")]    public partial class VideoControl : UserControl,IObjectSafety    {        public VideoControl()        {            InitializeComponent();        }        string videopath = Environment.CurrentDirectory;        Videos video;        IntPtr handle;        private void btnLogin_Click(object sender, EventArgs e)        {            if (btnLogin.Text == "登录")            {                try                {                    if (string.IsNullOrWhiteSpace(this.txtIP.Text))                    {                        MessageBox.Show("IP地址不能为空!");                        return;                    }                    if (string.IsNullOrWhiteSpace(this.txtUserID.Text))                    {                        MessageBox.Show("用户名不能为空!");                        return;                    }                    if (string.IsNullOrWhiteSpace(this.txtPwd.Text))                    {                        MessageBox.Show("密码不能为空!");                        return;                    }                    video = new Videos();                    if (video.Initialize(this.txtIP.Text, Convert.ToInt32(this.numericUpDown1.Value), this.txtUserID.Text, this.txtPwd.Text))                    {                        this.btnLogin.Text = "注销";                        MessageBox.Show("登录成功!");                        this.btnStart.Enabled = true;                        this.btnSave.Enabled = true;                    }                    else                    {                        MessageBox.Show("登录失败!");                    }                }                catch (Exception ee)                {                    MessageBox.Show("登录异常:" + ee.Message);                }            }            else if (btnLogin.Text == "注销")            {                try                {                    video.Dispose();                    this.btnLogin.Text = "登录";                    this.btnStart.Enabled = false;                    this.btnSave.Enabled = false;                }                catch (Exception ee)                {                    MessageBox.Show("注销异常:" + ee.Message);                }            }        }        private void btnStart_Click(object sender, EventArgs e)        {            try            {                string filename = txtFile.Text.Trim();                if (filename.IndexOfAny(Path.GetInvalidFileNameChars()) >= 0 || string.IsNullOrWhiteSpace(filename))                {                    MessageBox.Show("文件名含有非法字符或空格,请重新输入");                    txtFile.Focus();                    return;                }                video.Start(handle, filename + comboBox1.SelectedItem.ToString());                this.btnStart.Enabled = false;                this.btnSave.Enabled = true;            }            catch (Exception ee)            {                MessageBox.Show("异常:" + ee.Message);            }        }        private void btnSave_Click(object sender, EventArgs e)        {            try            {                if (video.End())                {                    MessageBox.Show("视频已保存!");                    this.btnStart.Enabled = true;                    this.btnSave.Enabled = false;                }                else                {                    MessageBox.Show("保存失败!");                    this.btnStart.Enabled = true;                    this.btnSave.Enabled = true;                }            }            catch (Exception ee)            { MessageBox.Show("异常:" + ee.Message); }        }        private void button1_Click(object sender, EventArgs e)        {            try            {                System.Diagnostics.Process.Start(videopath);            }            catch            { }        }        private void VideoControl_Load(object sender, EventArgs e)        {            this.comboBox1.SelectedItem = ".mp4";            this.handle = pictureBox1.Handle;            this.btnStart.Enabled = false;            this.btnSave.Enabled = false;        }        #region IObjectSafety 成员        private const string _IID_IDispatch = "{00020400-0000-0000-C000-000000000046}";        private const string _IID_IDispatchEx = "{a6ef9860-c720-11d0-9337-00a0c90dcaa9}";        private const string _IID_IPersistStorage = "{0000010A-0000-0000-C000-000000000046}";        private const string _IID_IPersistStream = "{00000109-0000-0000-C000-000000000046}";        private const string _IID_IPersistPropertyBag = "{37D84F60-42CB-11CE-8135-00AA004BB851}";        private const int INTERFACESAFE_FOR_UNTRUSTED_CALLER = 0x00000001;        private const int INTERFACESAFE_FOR_UNTRUSTED_DATA = 0x00000002;        private const int S_OK = 0;        private const int E_FAIL = unchecked((int)0x80004005);        private const int E_NOINTERFACE = unchecked((int)0x80004002);        private bool _fSafeForScripting = true;        private bool _fSafeForInitializing = true;        public int GetInterfaceSafetyOptions(ref Guid riid, ref int pdwSupportedOptions, ref int pdwEnabledOptions)        {            int Rslt = E_FAIL;            string strGUID = riid.ToString("B");            pdwSupportedOptions = INTERFACESAFE_FOR_UNTRUSTED_CALLER | INTERFACESAFE_FOR_UNTRUSTED_DATA;            switch (strGUID)            {                case _IID_IDispatch:                case _IID_IDispatchEx:                    Rslt = S_OK;                    pdwEnabledOptions = 0;                    if (_fSafeForScripting == true)                        pdwEnabledOptions = INTERFACESAFE_FOR_UNTRUSTED_CALLER;                    break;                case _IID_IPersistStorage:                case _IID_IPersistStream:                case _IID_IPersistPropertyBag:                    Rslt = S_OK;                    pdwEnabledOptions = 0;                    if (_fSafeForInitializing == true)                        pdwEnabledOptions = INTERFACESAFE_FOR_UNTRUSTED_DATA;                    break;                default:                    Rslt = E_NOINTERFACE;                    break;            }            return Rslt;        }        public int SetInterfaceSafetyOptions(ref Guid riid, int dwOptionSetMask, int dwEnabledOptions)        {            int Rslt = E_FAIL;            string strGUID = riid.ToString("B");            switch (strGUID)            {                case _IID_IDispatch:                case _IID_IDispatchEx:                    if (((dwEnabledOptions & dwOptionSetMask) == INTERFACESAFE_FOR_UNTRUSTED_CALLER) && (_fSafeForScripting == true))                        Rslt = S_OK;                    break;                case _IID_IPersistStorage:                case _IID_IPersistStream:                case _IID_IPersistPropertyBag:                    if (((dwEnabledOptions & dwOptionSetMask) == INTERFACESAFE_FOR_UNTRUSTED_DATA) && (_fSafeForInitializing == true))                        Rslt = S_OK;                    break;                default:                    Rslt = E_NOINTERFACE;                    break;            }            return Rslt;        }        #endregion    }}控件代码
namespace VideoHelper{    partial class VideoControl    {        ///          /// 必需的设计器变量。        ///         private System.ComponentModel.IContainer components = null;        ///          /// 清理所有正在使用的资源。        ///         /// 如果应释放托管资源,为 true;否则为 false。        protected override void Dispose(bool disposing)        {            if (disposing && (components != null))            {                components.Dispose();            }            base.Dispose(disposing);        }        #region 组件设计器生成的代码        ///          /// 设计器支持所需的方法 - 不要        /// 使用代码编辑器修改此方法的内容。        ///         private void InitializeComponent()        {            this.button1 = new System.Windows.Forms.Button();            this.comboBox1 = new System.Windows.Forms.ComboBox();            this.label4 = new System.Windows.Forms.Label();            this.txtFile = new System.Windows.Forms.TextBox();            this.btnSave = new System.Windows.Forms.Button();            this.btnStart = new System.Windows.Forms.Button();            this.btnLogin = new System.Windows.Forms.Button();            this.label3 = new System.Windows.Forms.Label();            this.txtPwd = new System.Windows.Forms.TextBox();            this.label2 = new System.Windows.Forms.Label();            this.txtUserID = new System.Windows.Forms.TextBox();            this.label1 = new System.Windows.Forms.Label();            this.numericUpDown1 = new System.Windows.Forms.NumericUpDown();            this.IP = new System.Windows.Forms.Label();            this.txtIP = new System.Windows.Forms.TextBox();            this.pictureBox1 = new System.Windows.Forms.PictureBox();            ((System.ComponentModel.ISupportInitialize)(this.numericUpDown1)).BeginInit();            ((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).BeginInit();            this.SuspendLayout();            //             // button1            //             this.button1.Cursor = System.Windows.Forms.Cursors.Hand;            this.button1.Font = new System.Drawing.Font("微软雅黑", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));            this.button1.Location = new System.Drawing.Point(377, 360);            this.button1.Name = "button1";            this.button1.Size = new System.Drawing.Size(138, 22);            this.button1.TabIndex = 58;            this.button1.Text = "打开视频存放位置";            this.button1.UseVisualStyleBackColor = true;            this.button1.Click += new System.EventHandler(this.button1_Click);            //             // comboBox1            //             this.comboBox1.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;            this.comboBox1.FlatStyle = System.Windows.Forms.FlatStyle.Flat;            this.comboBox1.Font = new System.Drawing.Font("微软雅黑", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));            this.comboBox1.FormattingEnabled = true;            this.comboBox1.Items.AddRange(new object[] {            ".mp4",            ".avi",            ".wmv",            ".3gp",            ".flv"});            this.comboBox1.Location = new System.Drawing.Point(303, 361);            this.comboBox1.Name = "comboBox1";            this.comboBox1.Size = new System.Drawing.Size(55, 25);            this.comboBox1.TabIndex = 57;            //             // label4            //             this.label4.AutoSize = true;            this.label4.Font = new System.Drawing.Font("微软雅黑", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));            this.label4.Location = new System.Drawing.Point(14, 360);            this.label4.Name = "label4";            this.label4.Size = new System.Drawing.Size(116, 17);            this.label4.TabIndex = 56;            this.label4.Text = "请输入视频文件名:";            //             // txtFile            //             this.txtFile.Location = new System.Drawing.Point(136, 360);            this.txtFile.Name = "txtFile";            this.txtFile.Size = new System.Drawing.Size(161, 21);            this.txtFile.TabIndex = 55;            //             // btnSave            //             this.btnSave.Cursor = System.Windows.Forms.Cursors.Hand;            this.btnSave.Font = new System.Drawing.Font("微软雅黑", 10.5F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));            this.btnSave.Location = new System.Drawing.Point(490, 298);            this.btnSave.Name = "btnSave";            this.btnSave.Size = new System.Drawing.Size(57, 45);            this.btnSave.TabIndex = 54;            this.btnSave.Text = "保存";            this.btnSave.UseVisualStyleBackColor = true;            this.btnSave.Click += new System.EventHandler(this.btnSave_Click);            //             // btnStart            //             this.btnStart.Cursor = System.Windows.Forms.Cursors.Hand;            this.btnStart.Font = new System.Drawing.Font("微软雅黑", 10.5F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));            this.btnStart.Location = new System.Drawing.Point(421, 298);            this.btnStart.Name = "btnStart";            this.btnStart.Size = new System.Drawing.Size(57, 45);            this.btnStart.TabIndex = 53;            this.btnStart.Text = "录制";            this.btnStart.UseVisualStyleBackColor = true;            this.btnStart.Click += new System.EventHandler(this.btnStart_Click);            //             // btnLogin            //             this.btnLogin.Cursor = System.Windows.Forms.Cursors.Hand;            this.btnLogin.Font = new System.Drawing.Font("微软雅黑", 10.5F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));            this.btnLogin.Location = new System.Drawing.Point(352, 298);            this.btnLogin.Name = "btnLogin";            this.btnLogin.Size = new System.Drawing.Size(57, 45);            this.btnLogin.TabIndex = 52;            this.btnLogin.Text = "登录";            this.btnLogin.UseVisualStyleBackColor = true;            this.btnLogin.Click += new System.EventHandler(this.btnLogin_Click);            //             // label3            //             this.label3.AutoSize = true;            this.label3.Font = new System.Drawing.Font("微软雅黑", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));            this.label3.Location = new System.Drawing.Point(172, 325);            this.label3.Name = "label3";            this.label3.Size = new System.Drawing.Size(44, 17);            this.label3.TabIndex = 51;            this.label3.Text = "密码:";            //             // txtPwd            //             this.txtPwd.Location = new System.Drawing.Point(221, 322);            this.txtPwd.Name = "txtPwd";            this.txtPwd.PasswordChar = '*';            this.txtPwd.Size = new System.Drawing.Size(115, 21);            this.txtPwd.TabIndex = 50;            this.txtPwd.Text = "8910jqk#";            this.txtPwd.UseSystemPasswordChar = true;            //             // label2            //             this.label2.AutoSize = true;            this.label2.Font = new System.Drawing.Font("微软雅黑", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));            this.label2.Location = new System.Drawing.Point(8, 322);            this.label2.Name = "label2";            this.label2.Size = new System.Drawing.Size(44, 17);            this.label2.TabIndex = 49;            this.label2.Text = "用户名";            //             // txtUserID            //             this.txtUserID.Location = new System.Drawing.Point(66, 322);            this.txtUserID.Name = "txtUserID";            this.txtUserID.Size = new System.Drawing.Size(100, 21);            this.txtUserID.TabIndex = 48;            this.txtUserID.Text = "admin";            //             // label1            //             this.label1.AutoSize = true;            this.label1.Font = new System.Drawing.Font("微软雅黑", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));            this.label1.Location = new System.Drawing.Point(172, 295);            this.label1.Name = "label1";            this.label1.Size = new System.Drawing.Size(44, 17);            this.label1.TabIndex = 47;            this.label1.Text = "端口:";            //             // numericUpDown1            //             this.numericUpDown1.Location = new System.Drawing.Point(222, 295);            this.numericUpDown1.Maximum = new decimal(new int[] {            65535,            0,            0,            0});            this.numericUpDown1.Minimum = new decimal(new int[] {            1,            0,            0,            0});            this.numericUpDown1.Name = "numericUpDown1";            this.numericUpDown1.Size = new System.Drawing.Size(114, 21);            this.numericUpDown1.TabIndex = 46;            this.numericUpDown1.Value = new decimal(new int[] {            8000,            0,            0,            0});            //             // IP            //             this.IP.AutoSize = true;            this.IP.Font = new System.Drawing.Font("微软雅黑", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));            this.IP.Location = new System.Drawing.Point(20, 295);            this.IP.Name = "IP";            this.IP.Size = new System.Drawing.Size(19, 17);            this.IP.TabIndex = 45;            this.IP.Text = "IP";            //             // txtIP            //             this.txtIP.Location = new System.Drawing.Point(66, 295);            this.txtIP.Name = "txtIP";            this.txtIP.Size = new System.Drawing.Size(100, 21);            this.txtIP.TabIndex = 44;            this.txtIP.Text = "192.168.1.64";            //             // pictureBox1            //             this.pictureBox1.Location = new System.Drawing.Point(5, 5);            this.pictureBox1.Name = "pictureBox1";            this.pictureBox1.Size = new System.Drawing.Size(542, 269);            this.pictureBox1.TabIndex = 43;            this.pictureBox1.TabStop = false;            //             // VideoControl            //             this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;            this.Controls.Add(this.button1);            this.Controls.Add(this.comboBox1);            this.Controls.Add(this.label4);            this.Controls.Add(this.txtFile);            this.Controls.Add(this.btnSave);            this.Controls.Add(this.btnStart);            this.Controls.Add(this.btnLogin);            this.Controls.Add(this.label3);            this.Controls.Add(this.txtPwd);            this.Controls.Add(this.label2);            this.Controls.Add(this.txtUserID);            this.Controls.Add(this.label1);            this.Controls.Add(this.numericUpDown1);            this.Controls.Add(this.IP);            this.Controls.Add(this.txtIP);            this.Controls.Add(this.pictureBox1);            this.Name = "VideoControl";            this.Size = new System.Drawing.Size(556, 398);            this.Load += new System.EventHandler(this.VideoControl_Load);            ((System.ComponentModel.ISupportInitialize)(this.numericUpDown1)).EndInit();            ((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).EndInit();            this.ResumeLayout(false);            this.PerformLayout();        }        #endregion        private System.Windows.Forms.Button button1;        private System.Windows.Forms.ComboBox comboBox1;        private System.Windows.Forms.Label label4;        private System.Windows.Forms.TextBox txtFile;        private System.Windows.Forms.Button btnSave;        private System.Windows.Forms.Button btnStart;        private System.Windows.Forms.Button btnLogin;        private System.Windows.Forms.Label label3;        private System.Windows.Forms.TextBox txtPwd;        private System.Windows.Forms.Label label2;        private System.Windows.Forms.TextBox txtUserID;        private System.Windows.Forms.Label label1;        private System.Windows.Forms.NumericUpDown numericUpDown1;        private System.Windows.Forms.Label IP;        private System.Windows.Forms.TextBox txtIP;        private System.Windows.Forms.PictureBox pictureBox1;    }}控件设计器代码

控件设计器代码

    至此,此项目结束。

    右键点击解决方案,添加新项目,如下,至于为什么建立两个项目,我一会儿在下面解释,

C#制作ActiveX控件中如何调用海康SDK的问题解决

    在HkHelper项目中添加类CHCNetSDK.cs,此类是海康提供的,可以在官网找到

    接下来,最重要的,项目属性设置如下,两个项目都要设置:

C#制作ActiveX控件中如何调用海康SDK的问题解决

C#制作ActiveX控件中如何调用海康SDK的问题解决

    至此,自定义控件已经完成,接下来就是打包,新建一个安装项目:

C#制作ActiveX控件中如何调用海康SDK的问题解决

    右键点击安装项目,“添加”->“项目输出”,并选择自定义控件的项目,然后确定

C#制作ActiveX控件中如何调用海康SDK的问题解决

 

     然后添加海康提供的SDK的库文件文件夹下的所有文件和文件夹到项目中,如下:

C#制作ActiveX控件中如何调用海康SDK的问题解决

    然后生成项目,会生成setup.exe和SetupVideo.msi两个文件,然后用打包文件,把这两个文件打包称cab文件就OK了

 

 打包文件一共三个cabarc.exe、build.bat、install.inf

build.bat文件:

"cabarc.exe"  n VideoSetup.cab SetupVideo.msi install.inf

install.inf文件:

[version]signature="$CHICAGO$"AdvancedINF=2.0[Setup Hooks]hook1=hook1[hook1]run=msiexec.exe /i "%EXTRACT_DIR%SetupVideo.msi" /qn

cabarc.exe是微软提供的工具

最后说一下为什么要分为两个项目去实现控件,那是因为如果在一个项目中的话,调用海康动态库的类CHCNetSDK.cs不能进行COM注册

 

 

以上就是C#制作ActiveX控件中如何调用海康SDK的问题解决的详细内容,更多请关注创想鸟其它相关文章!

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 chuangxiangniao@163.com 举报,一经查实,本站将立刻删除。
发布者:程序猿,转转请注明出处:https://www.chuangxiangniao.com/p/1432511.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2025年12月17日 08:21:54
下一篇 2025年12月16日 07:48:31

相关推荐

  • C#中匿名对象与var以及动态类型 dynamic的详解

    随着c#的发展,该语言内容不断丰富,开发变得更加方便快捷,c# 的锋利尽显无疑。c# 语言从诞生起就是强类型语言,这一性质到今天不曾改变,我想以后也不会变。既然是强类型语言,那编写任一程序均要求满足下面的基本条件: 1、变量声明必须指明其类型 2、变量类型明确后,其类型在Runtime亦不能改变 代…

    2025年12月17日
    000
  • 比较C#和JAVA中面向对象语法的区别

    面向对象是一种开发思想,最应该记住的一句话是万物皆对象。为了让程序更好的被理解和编写,把现实生活中描述事物的方式和思路融合进入,就成了面向对象的思想。把生活中的事物融合进程序中那么就需要描述,描述分为特征和行为两方面,而不同类别的对象特征和行为具有巨大的差异,为了更好的制定描述每一类事物的方式,那么…

    好文分享 2025年12月17日
    000
  • C#/.NET易错的几点

    1 及时释放资源      clr托管环境扮演了垃圾回收的角色,所以你不需要显式释放已创建对象所占用的内存。但这不意味着你可以忽略所有的使用过的对象。许多对象封装了其 他类型的系统资源(例如,磁盘文件,数据连接,网络端口)。保持这些资源的使用状态会急剧的耗尽系统的资源,削弱性能并且最终导致程序出错。…

    好文分享 2025年12月17日
    000
  • 介绍C#中的接口

    对于很多初学者来说是个很容易迷糊的东西,使用起来很简单,无非就是定义接口,接口里面包含一些属性、索引器、事件和一些没有修饰符的方法,也没有方法的具体实现代码;然后在类中继承该接口,实现该接口中的所有属性、索引器、事件和方法的具体实现的代码(其实接口中只能这几个,一般我们用到的只有属性和方法所以在这里…

    好文分享 2025年12月17日
    000
  • 有关C#工厂模式简单讲解

    一、 简单工厂(simple factory)模式simple factory模式根据提供给它的数据,返回几个可能类中的一个类的实例。通常它返回的类都有一个公共的父类和公共的方法。 simple factory模式实际上不是gof 23个设计模式中的一员。二、 simple factory模式角色与…

    好文分享 2025年12月17日
    000
  • C#多线程之Semaphore的使用详解

    这篇文章主要为大家详细介绍了c#多线程之semaphore用法,具有一定的参考价值,感兴趣的小伙伴们可以参考一下 Semaphore:可理解为允许线程执行信号的池子,池子中放入多少个信号就允许多少线程同时执行。 private static void MultiThreadSynergicWithS…

    好文分享 2025年12月17日
    000
  • C#中关于逆变和协变的详解

    这篇文章主要为大家详细介绍了c#逆变与协变的相关资料,具有一定的参考价值,感兴趣的小伙伴们可以参考一下 该文章中使用了较多的 委托delegate和Lambda表达式,如果你并不熟悉这些,请查看我的文章《委托与匿名委托》、《匿名委托与Lambda表达式》以便帮你建立完整的知识体系。 在C#从诞生到发…

    2025年12月17日
    000
  • C#中关于匿名委托和Lambda表达式的使用详解

    这篇文章主要为大家详细介绍了c#匿名委托与lambda表达式的相关资料,具有一定的参考价值,感兴趣的小伙伴们可以参考一下 通过使用匿名委托(匿名方法),使编程变得更加灵活,有关委托与匿名委托请参考我的前一篇Blog《委托与匿名委托》。 继续之前示例,代码如下: static void Main(st…

    好文分享 2025年12月17日
    000
  • C#中委托和匿名委托的具体介绍

    这篇文章主要为大家详细介绍了c#委托与匿名委托的相关资料,具有一定的参考价值,感兴趣的小伙伴们可以参考一下 本来是想写一篇《委托与lambda表达式的前世今生》,但仅委托部分已经写了很多内容,于是就此分开关于Lambda表达是的内容后续再写吧。 不知道Lambda表达式是谁发明的,只记得第一次接触L…

    好文分享 2025年12月17日
    000
  • C#如何使用ILGenerator实现动态生成函数的实例

    这篇文章主要介绍了c#使用ilgenerator动态生成函数的简单代码,需要的朋友可以参考下 游戏服务器里面总是有一大堆的配置文件需要读取, 而且这些配置文件的读取: * 要不然做成弱类型的, 就是一堆字符串或者数字, 不能看出来错误(需要重新检测一次) * 要不然做成强类型的, 每种类型都需要自己…

    好文分享 2025年12月17日
    000
  • C#如何连接到sql server2008数据库的示例分享

    这篇文章主要介绍了c#连接到sql server2008数据库的实例代码,需要的朋友可以参考下 废话不多说了,直接给大家贴代码了,具体代码如下所示: namespace MyFirstApp{ class Program { static void Main(string[] args) { Sql…

    好文分享 2025年12月17日
    000
  • C#动态数据绘图graphic的实现方法介绍

    这篇文章主要介绍了c#实现动态数据绘图graphic的方法,结合实例形式分析了c#根据动态数据绘制2d数据表格的相关操作技巧,需要的朋友可以参考下 本文实例讲述了C#实现动态数据绘图graphic的方法。分享给大家供大家参考,具体如下: using System;using System.Colle…

    2025年12月17日
    000
  • C#如何实现loading提示控件简单的实例

    本文通过实例代码给大家介绍了c#实现简单的loading提示控件功能,代码非常简单,具有参考借鉴价值,需要的朋友参考下吧 自己画一个转圈圈的控件 using System;using System.Collections.Generic;using System.ComponentModel;usi…

    好文分享 2025年12月17日
    000
  • C#中Observer观察者模式如何解决牛顿童鞋成绩问题的方法

    这篇文章主要介绍了c#设计模式之observer观察者模式解决牛顿童鞋成绩问题,简单讲述了观察者模式的原理并结合具体实例形式分析了使用观察者模式解决牛顿童鞋成绩问题的具体步骤相关操作技巧,并附带demo源码供读者下载参考,需要的朋友可以参考下 本文实例讲述了C#设计模式之Observer观察者模式解…

    2025年12月17日
    000
  • .NET Core2.0小技巧之MemoryCache问题修复解决的方法(图)

    这篇文章主要给大家介绍了关于.net core 2.0迁移小技巧之memorycache问题修复解决的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧。 前言 大家应该都知道,对于传统的.NET Framework项目而言…

    2025年12月17日
    000
  • C#基础之操作优化实例教程

    对数据的查询,删除等基本操作是任何编程语言都会涉及到的基础,因此,研究了一下c#中比较常用的数据操作类型,并顺手做个笔记. List查询时,若是处理比较大的数据则使用HashSet类,因为List是基于线性表操作的.但其内嵌了二分查找(BinarySearch),因此,也可以在存储完之后进行排序,随…

    2025年12月17日
    000
  • 分享一个IoC入门教程实例

    spring.net包括控制反转(ioc) 和面向切面(aop),这篇文章主要说下ioc方面的入门。 一、首先建立一个MVC项目名称叫SpringDemo,然后用NuGet下载spring(我用的是Spring.Net NHibernate 4 support) 二、类设计,在Models文件夹下面…

    2025年12月17日
    000
  • C#串口通信的实例教程

    因为参加一个小项目,需要对继电器进行串口控制,所以这两天学习了基本的串口编程。同事那边有java的串口通信包,不过是从网上下载的,比较零乱,难以准确掌握串口通信的流程和内含。因此,个人通过学习网上大牛的方法,利用c#实现了基本的串口通信编程。下面对学习成果进行总结归纳,希望对大家有所帮助。 一、串口…

    2025年12月17日
    000
  • C#中多线程之Thread类详解

    使用system.threading.thread类可以创建和控制线程。 常用的构造函数有: // 摘要: // 初始化 System.Threading.Thread 类的新实例,指定允许对象在线程启动时传递给线程的委托。//// 参数: // start:// System.Threading.…

    2025年12月17日
    000
  • 总结.NET平台上一些常用的框架

    分布式缓存框架: Microsoft Velocity:微软自家分布式缓存服务框架。 Memcahed:一套分布式的高速缓存系统,目前被许多网站使用以提升网站的访问速度。 Redis:是一个高性能的KV数据库。 它的出现很大程度补偿了Memcached在某些方面的不足。 EnyimMemcached…

    2025年12月17日
    000

发表回复

登录后才能评论
关注微信