C#中方括弧[]的文法及作用介紹

來源:互聯網
上載者:User

1. C#實現.NET組件與COM組件的互操作

[DllImport("kernel32.dll")]這叫引入kernel32.dll這個動態串連庫。
這個動態串連庫裡麵包含了很多WindowsAPI函數,如果你想使用這面的函數,就需要這麼引入。舉個例子:
[DllImport("kernel32.dll")]
private static extern void 函數名(參數,[參數]);
函數名就是一個屬於kernel32.dll裡的一個函數。完了你就可以用那個函數了。

.NET組件中使用目前存在的COM組件
對於.NET來講,使用COM組件就要簡單一些。..NET提供了大量的類庫來方便的實現同COM的相互操作,其中很重要的一個名稱空間就是:System.Runtime.InteropServices。通過這個名稱空間的名字我們也可以從字面上看出,"互操作服務"。System.Runtime.InteropServices這個名稱空間提供了一系列的類來對COM對象進行操作。

需要注意的是,在調用COM組件之前,我們需要在.NET程式中引用名稱空間:System.Runtime.InteropServices 。因為我們需要使用這個名稱空間所提供的一個方法:DllImport。

例子: 記憶體,硬碟的利用率

複製代碼 代碼如下:using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
namespace Windows.Help
{
public partial class SystemInfo : Form
{
public SystemInfo()
{
InitializeComponent();
}
[DllImport("kernel32")]
public static extern void GetWindowsDirectory(StringBuilder WinDir, int count);
[DllImport("kernel32")]
public static extern void GetSystemDirectory(StringBuilder SysDir, int count);
[DllImport("kernel32")]
public static extern void GetSystemInfo(ref CPU_INFO cpuinfo);
[DllImport("kernel32")]
public static extern void GlobalMemoryStatus(ref MEMORY_INFO meminfo);
[DllImport("kernel32")]
public static extern void GetSystemTime(ref SYSTEMTIME_INFO stinfo);
//定義CPU的資訊結構
[StructLayout(LayoutKind.Sequential)]
public struct CPU_INFO
{
public uint dwOemId;
public uint dwPageSize;
public uint lpMinimumApplicationAddress;
public uint lpMaximumApplicationAddress;
public uint dwActiveProcessorMask;
public uint dwNumberOfProcessors;
public uint dwProcessorType;
public uint dwAllocationGranularity;
public uint dwProcessorLevel;
public uint dwProcessorRevision;
}
//定義記憶體的資訊結構
[StructLayout(LayoutKind.Sequential)]
public struct MEMORY_INFO
{
public uint dwLength;
public uint dwMemoryLoad;
public uint dwTotalPhys;
public uint dwAvailPhys;
public uint dwTotalPageFile;
public uint dwAvailPageFile;
public uint dwTotalVirtual;
public uint dwAvailVirtual;
}
//定義系統時間的資訊結構
[StructLayout(LayoutKind.Sequential)]
public struct SYSTEMTIME_INFO
{
public ushort wYear;
public ushort wMonth;
public ushort wDayOfWeek;
public ushort wDay;
public ushort wHour;
public ushort wMinute;
public ushort wSecond;
public ushort wMilliseconds;
}
private void button1_Click(object sender, EventArgs e)
{
//調用GetWindowsDirectory和GetSystemDirectory函數分別取得Windows路徑和系統路徑
const int nChars = 128;
StringBuilder Buff = new StringBuilder(nChars);
GetWindowsDirectory(Buff, nChars);
WindowsDirectory.Text = "Windows路徑:" + Buff.ToString();
GetSystemDirectory(Buff, nChars);
SystemDirectory.Text = " 系統路徑:" + Buff.ToString();
//調用GetSystemInfo函數擷取CPU的相關資訊
CPU_INFO CpuInfo;
CpuInfo = new CPU_INFO();
GetSystemInfo(ref CpuInfo);
NumberOfProcessors.Text = "本電腦中有" + CpuInfo.dwNumberOfProcessors.ToString() + "個CPU";
ProcessorType.Text = "CPU的類型為" + CpuInfo.dwProcessorType.ToString();
ProcessorLevel.Text = "CPU等級為" + CpuInfo.dwProcessorLevel.ToString();
OemId.Text = "CPU的OEM ID為" + CpuInfo.dwOemId.ToString();
PageSize.Text = "CPU中的頁面大小為" + CpuInfo.dwPageSize.ToString();
//調用GlobalMemoryStatus函數擷取記憶體的相關資訊
MEMORY_INFO MemInfo;
MemInfo = new MEMORY_INFO();
GlobalMemoryStatus(ref MemInfo);
MemoryLoad.Text = MemInfo.dwMemoryLoad.ToString() + "%的記憶體正在使用";
TotalPhys.Text = "實體記憶體共有" + MemInfo.dwTotalPhys.ToString() + "位元組";
AvailPhys.Text = "可使用的實體記憶體有" + MemInfo.dwAvailPhys.ToString() + "位元組";
TotalPageFile.Text = "分頁檔總大小為" + MemInfo.dwTotalPageFile.ToString() + "位元組";
AvailPageFile.Text = "尚可分頁檔大小為" + MemInfo.dwAvailPageFile.ToString() + "位元組";
TotalVirtual.Text = "總虛擬記憶體有" + MemInfo.dwTotalVirtual.ToString() + "位元組";
AvailVirtual.Text = "未用虛擬記憶體有" + MemInfo.dwAvailVirtual.ToString() + "位元組";
//調用GetSystemTime函數擷取系統時間資訊
SYSTEMTIME_INFO StInfo;
StInfo = new SYSTEMTIME_INFO();
GetSystemTime(ref StInfo);
Date.Text = StInfo.wYear.ToString() + "年" + StInfo.wMonth.ToString() + "月" + StInfo.wDay.ToString() + "日";
Time.Text = (StInfo.wHour + 8).ToString() + "點" + StInfo.wMinute.ToString() + "分" + StInfo.wSecond.ToString() + "秒";
}
private void button2_Click(object sender, EventArgs e)
{
this.Close();
}
}
}

2. 數組、索引器中的應用

type 類型。
array 數組。

indexexpr 索引運算式。
備忘
數群組類型是一種後跟 [] 的類型:
int[] fib; // fib is of type int[], "array of int"
fib = new int[100]; // create a 100-element int array
若要訪問數組的一個元素,則用方括弧括起所需元素的索引:
fib[0] = fib[1] = 1;
for( int i=2; i <100; ++i ) fib[i] = fib[i-1] + fib[i-2];
如果數組索引超出範圍,則會引發異常。
不能重載數組索引運算子;但類型可以定義包含一個或多個參數的索引器和屬性。索引器參數括在方括弧中(就像數組索引一樣),但索引器參數可聲明為任何類型(與數組索引不同,數組索引必須為整數)。
例如,.NET Framework 定義一個雜湊表類型,該類型將鍵和任意類型的值關聯在一起。
Collections.Hashtable h = new Collections.Hashtable();
h["a"] = 123; // note: using a string as the index

3. 方括弧用於指定屬性

attribute(AllowMultiple=true)]
public class Attr {
}
可使用方括弧來索引指標後面的儲存位置(請參見 A.2 指標類型):
unsafe fixed ( int* p = fib ) // p points to fib from earlier example
{
p[0] = p[1] = 1;
for( int i=2; i <100; ++i ) p[i] = p[i-1] + p[i-2];
}
不執行邊界檢查。
[ToolBoxItem(false)]表示不在IDE工具箱的控制項集合中顯示。

[ParseChildren(true)]
它是用來告訴解析器 頁面聲明文法中位於 指定標籤內的內容(子標籤)是否是看作該控制項的屬性還是當作一個子控制項的標籤。
true 這裡true 是當作子標籤的意思。 ToolBoxData 的意思是當你將這個控制項從tool box 中拖放到WEBFORM中時在aspx檔案的
HTML代碼中添加的對該控制項的定義。這裡的控制項是 : kj_gridview {0}是控制項的標記的首碼
就是你托這個控制項到頁面上 時候 他就自動添加<{0}:Div runat=server> 這個{0}是你定義的

複製代碼 代碼如下:1、[StandardParameter(“ProcessID“)]
2、[System.Web.Services.WebMethod(EnableSession=true)]
3、[Guid(“D301882E-46D1-4e83-BF15-67028B94A68D“)]
4、[Category(“Drp“)]
5、[DBDefineAttribute(“Banks.XML“)]
[Serializable]
6、[DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]

這些是類或方法的屬性
這個在大型的軟體開發中是很有用的,如項目中要用到的一個模組是通用的,我們就可以將其單獨抽出來做成控制項,這個時候類或是方法的屬性就有用了
加上這些後使控制項類在使用的時候就會有相應的列項提示等等

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.