重慶分公司,新征程啟航
為企業提供網站建設、域名注冊、服務器等服務
為企業提供網站建設、域名注冊、服務器等服務
type是vb里面定義結構的關鍵字,structure是vb.net中定義的關鍵字,使用的語言環境不一樣
我們提供的服務有:網站設計、網站建設、微信公眾號開發、網站優化、網站認證、驛城ssl等。為1000+企事業單位解決了網站和推廣的問題。提供周到的售前咨詢和貼心的售后服務,是有科學管理、有技術的驛城網站制作公司
如果是要判斷引用類型可以用TypeOf來判斷
Dim s = "666"
If TypeOf (s) Is String Then
Debug.Print("string")
Else
Debug.Print("not string")
End If
如果不知道是否是引用類型,可以這樣判斷:
Dim s = 666
If VarType(s) = VariantType.String Then
Debug.Print("string")
Else
Debug.Print("not string")
End If
或者:
Dim s = 666
If s.GetType = "".GetType Then
Debug.Print("string")
Else
Debug.Print("not string")
End If
type 0 即是 type小于或大于0,即type不等于0,C里面的寫法是type!=0
這個功能實現起來其實也很簡單,就是通過反射去讀取 DescriptionAttribute 的 Description 屬性的值,代碼如下所示:
/// summary
/// 返回枚舉項的描述信息。
/// /summary
/// param name="value"要獲取描述信息的枚舉項。/param
/// returns枚舉想的描述信息。/returns
public static string GetDescription(Enum value)
{
Type enumType = value.GetType();
// 獲取枚舉常數名稱。
string name = Enum.GetName(enumType, value);
if (name != null)
{
// 獲取枚舉字段。
FieldInfo fieldInfo = enumType.GetField(name);
if (fieldInfo != null)
{
// 獲取描述的屬性。
DescriptionAttribute attr = Attribute.GetCustomAttribute(fieldInfo,
typeof(DescriptionAttribute), false) as DescriptionAttribute;
if (attr != null)
{
return attr.Description;
}
}
}
return null;
}
這段代碼還是很容易看懂的,這里取得枚舉常數的名稱使用的是 Enum.GetName() 而不是 ToString(),因為前者更快,而且對于不是枚舉常數的值會返回 null,不用進行額外的反射。
當然,這段代碼僅是一個簡單的示例,接下來會進行更詳細的分析。