如何在C#中为字体系列创建枚举 [英] How to create enum for font family in C#

查看:119
本文介绍了如何在C#中为字体系列创建枚举的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

亲爱的所有人,



我有一个Windows应用程序,我们正在自动化MS-OFFICE 2013/16。



现在,我们正在尝试比较字体系列,因为我想创建枚举字体系列。



WordInterop中是否有字体系列枚举?



我尝试了什么:



因为字体名称有空间,所以我卡住了如何创建这个枚举

 public enum FontFamilies 
{
Arial,
Arial Narrow // Error
}





任何人都可以帮助我...





谢谢

解决方案

你有几个选择。



首先你可以枚举系统上安装的所有字体 - 请参阅如何:枚举已安装的字体 [ ^ ]



如果你想建立自己的枚举,你可以有像空格一样的描述这个(参考: c# - 我的枚举可以有友好的名字吗? [ ^ ])

  //  需要使用System.ComponentModel;  
public enum FontFamilies
{
[描述( Ariel)]
Ariel,
[描述( Arial Narrow)]
ArialNarrow,
[描述( Times New Roman)]
TimesNewRoman
};

要获得这些描述,您可以这样做(与上面相同的参考):

  //  需要使用System.Reflection;  
public static string GetDescription(Enum value
{
类型type = value .GetType();
string name = Enum.GetName(type, value );
if (name!= null
{
FieldInfo字段= type.GetField(name);
if (field!= null
{
DescriptionAttribute attr =
Attribute.GetCustomAttribute(字段,
typeof (DescriptionAttribute)) as DescriptionAttribute ;
if (attr!= null
{
return attr.Description;
}
}
}
返回 null ;
}





另一种方法是扩展 enum 请参阅 - c# - 我的枚举可以有友好的名字吗? - Stack Overflow [ ^ ]

  public   enum  FontFamilies2 
{
Ariel,
ArialNarrow,
TimesNewRoman
};

  public   static   class  ExtensionMethods 
{
public static string EnumValue( FontFamilies2 e)
{
开关(e)
{
case FontFamilies2.Ariel:
return Ari EL;
case FontFamilies2.ArialNarrow:
return Ariel Narrow;
case FontFamilies2.TimesNewRoman:
return Times New Roman;
}
return 其中你找到了那个价值;
}
}

这就是你分别访问这些描述的方法:

 System.Diagnostics.Debug.Print(GetDescription(FontFamilies.TimesNewRoman) ); 
System.Diagnostics.Debug.Print(FontFamilies2.TimesNewRoman.EnumValue());







假设你已经去了 [描述...] 属性,那么你可以这样做:

  //  需要使用System.Collections.Generic;  
var fonts = new List< string>();
foreach var x in Enum.GetValues(enumType: typeof (FontFamilies)))
fonts.Add(GetDescription(x as 枚举));

或Linq版

  var  fonts =(来自 对象 x   Enum.GetValues(enumType: typeof (FontFamilies))选择 GetDescription(x  as 枚举) ).ToList(); 


Dear All,

I have an windows application, in which we are automating MS-OFFICE 2013/16.

Now, we are trying to compare the Font Family, for that i want to create enum font family.

Is there font family enum in WordInterop?

What I have tried:

Because the font names have space, so i got stuck how to create this enum

public enum FontFamilies
{
  Arial,
  Arial Narrow//Error
}



can anyone plz help me...


Thanks

解决方案

You have several options.

Firstly you can enumerate all of the fonts installed on your system - see How to: Enumerate Installed Fonts[^]

If you want to build your own enum, you can have descriptions with spaces like this (ref:c# - Can my enums have friendly names?[^])

//Requires using System.ComponentModel;
public enum FontFamilies
{
    [Description("Ariel")]
    Ariel,
    [Description("Arial Narrow")]
    ArialNarrow,
    [Description("Times New Roman")]
    TimesNewRoman
};

To get these descriptions you can do this (same ref as above):

//Requires using System.Reflection;
public static string GetDescription(Enum value)
{
    Type type = value.GetType();
    string name = Enum.GetName(type, value);
    if (name != null)
    {
        FieldInfo field = type.GetField(name);
        if (field != null)
        {
            DescriptionAttribute attr =
                        Attribute.GetCustomAttribute(field,
                        typeof(DescriptionAttribute)) as DescriptionAttribute;
            if (attr != null)
            {
                return attr.Description;
            }
        }
    }
    return null;
}



An alternative method is to extend enum see - c# - Can my enums have friendly names? - Stack Overflow[^]

public enum FontFamilies2
{
     Ariel,
     ArialNarrow,
     TimesNewRoman
};

public static class ExtensionMethods
{
    public static string EnumValue(this FontFamilies2 e)
    {
        switch (e)
        {
            case FontFamilies2.Ariel:
                return "Ariel";
            case FontFamilies2.ArialNarrow:
                return "Ariel Narrow";
            case FontFamilies2.TimesNewRoman:
                return "Times New Roman";
        }
        return "Where did you find that value";
   }
}

And this is how you would access those descriptions respectively:

System.Diagnostics.Debug.Print(GetDescription(FontFamilies.TimesNewRoman));
System.Diagnostics.Debug.Print(FontFamilies2.TimesNewRoman.EnumValue());



[EDIT OP now states they want this description as a list]
Assuming you have gone for the [Description...] attribute then you can do this:

//Requires using System.Collections.Generic;
var fonts = new List<string>();
foreach (var x in Enum.GetValues(enumType: typeof (FontFamilies)))
    fonts.Add(GetDescription(x as Enum));

or the Linq version

var fonts = (from object x in Enum.GetValues(enumType: typeof (FontFamilies)) select GetDescription(x as Enum)).ToList();


这篇关于如何在C#中为字体系列创建枚举的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆