C#开发实用知识点总结

总结一些C#开发过程中实用的知识点,聚沙成塔,一点一滴积累。

#注释信息在提示时换行显示

/// <summary> 
/// 基类(第1行) 
///<para>说明:(第2行)</para> 
/// </summary>

 

#拷贝窗体到另一个项目

1. 在资源管理器中,右键单击 新工程的名称;
2. 在其右键菜单中,添加现有项,弹出的对话框中,选择添加原来的Form文件,包括了三个文件Form1.cs、Form1.Designer.cs、Form1.resx;
3. 添加进来后,修改错误并编译;
4. 右击Form1.cs,选择查看设计器,可能看见的UI效果和原先不一样。这时,需将这三个窗体文件从项目中排除,最后仅仅将Form1.cs添加到工程中即可。

#移除添加的事件

public delegate void OutputMsgEventHandle(object msg);
public event OutputMsgEventHandle OutputtedMsg;

if (OutputtedMsg != null)
{
    Delegate[] deles = OutputtedMsg.GetInvocationList();
    foreach (Delegate dele in deles)
    {
        OutputtedMsg -= dele as OutputMsgEventHandle;
    }
}

#移除添加的委托

public delegate void OutputMsgEventHandle(object msg);

OutputMsgEventHandle output = new OutputMsgEventHandle(method);

Delegate[] deles = output.GetInvocationList();
foreach (Delegate dele in deles)
{
    OutputtedMsg -= dele as OutputMsgEventHandle;
}

#判断是否已打开对话框

foreach (Form fm in System.Windows.Forms.Application.OpenForms)
{
    if (fm.Name == "FormBdcSearch")//对话框名称,不是标题
    {
        fm.Activate();
        fm.WindowState = FormWindowState.Normal;
        return;
    }
}

#获取程序路径

//获取新的 Process 组件并将其与当前活动的进程关联的主模块的完整路径,包含文件名(进程名)。
string str = System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName;
//result: X:xxxxxxxxx.exe(.exe文件所在的目录 +.exe文件名)

//获取当前 Thread 的当前应用程序域的基目录
string str = System.AppDomain.CurrentDomain.BaseDirectory;
//result: X:xxxxxx (.exe文件所在的目录 + "")

//获取和设置包含该应用程序的目录的名称
string str = System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase;
//result: X:xxxxxx (.exe文件所在的目录 + "")

//获取启动了应用程序的可执行文件的路径,不包括可执行文件的名称。
string str = System.Windows.Forms.Application.StartupPath;
//result: X:xxxxxx(.exe文件所在的目录)

#判断dll是Debug还是Release

using System.Reflection;
using System.IO;

private bool IsAssemblyDebugBuild(string filepath)
{
    Assembly assembly = Assembly.LoadFile(Path.GetFullPath(filepath));
    return assembly.GetCustomAttributes(false).Any(x =>
                (x as DebuggableAttribute) != null ?
                (x as DebuggableAttribute).IsJITTrackingEnabled : false);
}

#线程与主进程通信

using System.Threading;

SynchronizationContext synContext = SynchronizationContext.Current;
Thread thread = new Thread(new ThreadStart(DoWork));
thread.Start();

public void DoWork()
{
    //线程内部调用,发送消息(同步/异步)到主线程,这样就保证了Message_Callback肯定在主线程中执行
    //synContext.Post(new SendOrPostCallback(Message_Callback), parameter);
    synContext.Send(new SendOrPostCallback(Message_Callback), parameter);
}

public void Message_Callback(object obj)
{
    //主线程的同步对象SynchronizationContext.Send调用,这个是在主线程中执行的
    doSomething ...(调用/修改界面、控件等完全不用invoke)
}

#自定义控件主要事项

一般在设计时打开设计视图报"未将对象引用设置到对象的实例",基本上都是你在用户控件的构造方法及Form Load事件中写入了计算的代码。若必须如此,在设计时可以加入以下语句,防止设计视图无法打开。

public ucAddUser()
{
    InitializeComponent();
    //在设计模式下直接返回,不再执行下面的代码,只有在程序运行时才执行后面的代码
    if (DesignMode) return;
    //如果还是有问题,则添加下面代码(谨慎添加),当运行的进程是VS Studio时,不执行后面的代码
    if (string.Compare(System.Diagnostics.Process.GetCurrentProcess().ProcessName, "devenv") == 0) return;
}

#警告消息关闭与开启

pragma warning disable 1591 // 禁用
pragma warning restore 1591