小编tcn*_*lan的帖子

C#与VB.Net中的命名空间引用

在VB.Net中你可以做如下的事情,没有任何问题...只是忽略这是一个非常无用的类的事实:-)


Imports System

Public Class Class1
    Public Shared Function ArrayToList(ByVal _array() As String) As Collections.Generic.List(Of String)
        Return New Collections.Generic.List(Of String)(_array)
    End Function
End Class
Run Code Online (Sandbox Code Playgroud)

但是如果你在C#中做同样的事情......


using System;

public class Class1
{
    public static Collections.Generic.List ArrayToList(string[] _array)
    {
        return new Collections.Generic.List(_array);
    }
}
Run Code Online (Sandbox Code Playgroud)

您将在"Collections.Generic.List"返回的行上收到错误,说"找不到类型或命名空间名称'Collections'(您是否缺少using指令或程序集引用?)"

我知道你必须有一个使用System.Collections.Generic的using指令才能使用List,但我不知道为什么.我也不明白为什么我在函数声明中没有得到相同的错误,只是在return语句中.

我希望有人可以解释这一点,甚至可以将我推荐给一个解释它的technet页面.我四处寻找,但找不到任何解释这个概念的东西.

编辑:请注意,问题实际上是关于子命名空间的引用,例如在示例中能够引用系统中的集合.

c# vb.net using using-directives

9
推荐指数
2
解决办法
3277
查看次数

.Net枚举winforms字体样式?

我一直在寻找一种方法来列出使用.Net框架的给定字体的有效字体样式(即使我必须使用gdi32或其他一些API),因为并非所有字体都属于System.Drawing.FontStyle枚举值(粗体,斜体,常规,罢工,下划线).Segoe UI是一种TrueType Microsoft字体,字体样式为:Regular,Semibold,Light,Bold,Italic和BoldItalic,这是一个不符合要求的字体的完美示例.另一个例子是Arial,它具有:Regular,Narrow,Italic,Bold,Bold Italic,Narrow Bold,Narrow Bold Italic和Narrow Italic.

在Windows 7中(可能也是vista,但我没有要检查的机器)当您打开资源管理器并浏览到%SystemRoot%\ Fonts时,您将看到一个名为"字体样式"的列,其中列出了所有可用的样式对于每种字体,它告诉我有一种方法可以做到这一点,至少通过API调用.

最后,我希望枚举FontFamily列表,然后列出每个系列的每种字体样式.下面列出了所有字体系列的示例代码,如果有人可以提供帮助列出每个家庭可用的字体样式,我将不胜感激.如果我以错误的方式解决这个问题,我绝对愿意接受建议.

Drawing.Text.InstalledFontCollection ifc = new Drawing.Text.InstalledFontCollection();
foreach ( FontFamily ff in ifc.Families )
{
    Console.WriteLine(ff.ToString());
    // Something like this would be nice, but AFAIK nothing similar exists
    /*
    foreach ( FontStyle style in ff.Styles )
        Console.WriteLine(style.ToString());
    */
}
Run Code Online (Sandbox Code Playgroud)

.net c# fonts styles enumeration

7
推荐指数
1
解决办法
3939
查看次数

标签 统计

c# ×2

.net ×1

enumeration ×1

fonts ×1

styles ×1

using ×1

using-directives ×1

vb.net ×1