C#如何访问静态类List<>从另一个班级

时间:2016-05-07 23:35:28

标签: c# list static office-interop static-members

使用C#,我有一个静态类,它有一个自定义类型的静态列表。这是自定义类型:

public class LanguageItem
{
    public Word.WdLanguageID Id { get; set; }
    public string Name { get; set; }

    public LanguageItem(string name, int id)
    {
        Id = (Word.WdLanguageID)id;
        Name = name;
    }
}

这是使用此类型的静态类:

public static class LanguageList
{            
    public static List<LanguageItem> _languageList;

    static LanguageList()
    {
        _languageList.Add(new LanguageItem("Arabic", 1025));
        _languageList.Add(new LanguageItem("Bulgarian", 1026));
        _languageList.Add(new LanguageItem("Catalan", 1027));
        _languageList.Add(new LanguageItem("TraditionalChinese", 1028));
        _languageList.Add(new LanguageItem("Czech", 1029));
        _languageList.Add(new LanguageItem("Danish", 1030));
        _languageList.Add(new LanguageItem("German", 1031));
        _languageList.Add(new LanguageItem("Greek", 1032));
        _languageList.Add(new LanguageItem("EnglishUS", 1033));
        _languageList.Add(new LanguageItem("Spanish", 1034));
        _languageList.Add(new LanguageItem("Finnish", 1035));
        _languageList.Add(new LanguageItem("French", 1036));
        _languageList.Add(new LanguageItem("Hebrew", 1037));
        _languageList.Add(new LanguageItem("Hungarian", 1038));
        _languageList.Add(new LanguageItem("Icelandic", 1039));
        _languageList.Add(new LanguageItem("Italian", 1040));
        _languageList.Add(new LanguageItem("Japanese", 1041));
        _languageList.Add(new LanguageItem("Korean", 1042));
        _languageList.Add(new LanguageItem("Dutch", 1043));
    }

    public static List<LanguageItem> LanguageListItems
    {
        get
        {
            return _languageList;
        }
        private set
        {
            _languageList = value;
        }
    }
}

我要做的是使用其他类中的列表,从列表中返回项目。我想指定Name,我想恢复Id

我试着用这个:

using Word = Microsoft.Office.Interop.Word;

Word.Application oWord = new Word.Application();

oWord.Selection.LanguageID = LanguageList.Single(lang => lang.Name == strTgtLanguage).Id;

但是我收到的编译错误是:

  

&#39; LanguageList&#39;不包含&#39; Single&#39;

的定义

我尝试查看其他类似的帖子,例如How to access all specific versions of a generic static class in C#?How to access member of a static class which is inside another static class以及其他帖子,但我无法根据这些帖子弄清楚。

另外我使用硬编码枚举的原因是因为如果我使用COM对象Office.Interop.Word,则需要永久(超过2分钟)将所有250多个项目添加到列表中。

有人可以帮助我指出我做错了什么,为什么我无法访问列表,或者是否有更好的方法?提前谢谢。

3 个答案:

答案 0 :(得分:4)

我认为你的方法不合适。

如果您对列表进行硬编码,那么为什么要设置一个setter?此外,列表应该是不可变的,对吗?

我相信这会更好地满足您的需求:

    public static class LanguageList
{
    private static readonly List<LanguageItem> _languageList = new List<LanguageItem>(new[]
    {
        new LanguageItem("Arabic", 1025),
        new LanguageItem("Bulgarian", 1026),
        new LanguageItem("Catalan", 1027),
        new LanguageItem("TraditionalChinese", 1028),
        new LanguageItem("Czech", 1029),
        new LanguageItem("Danish", 1030),
        new LanguageItem("German", 1031),
        new LanguageItem("Greek", 1032),
        new LanguageItem("EnglishUS", 1033),
        new LanguageItem("Spanish", 1034),
        new LanguageItem("Finnish", 1035),
        new LanguageItem("French", 1036),
        new LanguageItem("Hebrew", 1037),
        new LanguageItem("Hungarian", 1038),
        new LanguageItem("Icelandic", 1039),
        new LanguageItem("Italian", 1040),
        new LanguageItem("Japanese", 1041),
        new LanguageItem("Korean", 1042),
        new LanguageItem("Dutch", 1043),
    });

      public static IEnumerable<LanguageItem> GetLanguages ()
    {
        return _languageList;
    }

    public static LanguageItem GetLanguageItem(string languageName)
    {
        return _languageList.SingleOrDefault(li => li.Name.Equals(languageName));
    }
}

答案 1 :(得分:1)

来自<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:jpa="http://www.springframework.org/schema/data/jpa" xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa.xsd "> <context:property-placeholder location="classpath:app.properties" ignore-unresolvable="true"/> <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <property name="driverClassName" value="${class}" /> <property name="url" value="${url}" /> <property name="username" value="${user}" /> <property name="password" value="${password}" /> </bean> <bean id="jpaVendorAdapter" class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"> </bean> <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"> <property name="dataSource" ref="dataSource"/> <property name="jpaVendorAdapter" ref="jpaVendorAdapter"/> <property name="packagesToScan"> <value>com.mypackage.new.entities</value> </property> <property name="jpaProperties"> <props> <prop key="hibernate.hbm2ddl.auto">validate</prop> <prop key="hibernate.dialect">org.hibernate.dialect.MySQL5InnoDBDialect</prop> <prop key="hibernate.show_sql">${hibernate.show_sql}</prop> <prop key="hibernate.format_sql">${hibernate.format_sql}</prop> <prop key="hibernate.id.new_generator_mappings">true</prop> <prop key="hibernate.max_fetch_depth">3</prop> </props> </property> </bean> <!-- Enables the Hibernate @Transactional programming model --> <tx:annotation-driven transaction-manager="transactionManager" /> <bean id="jpaDialect" class="org.springframework.orm.jpa.vendor.HibernateJpaDialect" /> <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager"> <property name="entityManagerFactory" ref="entityManagerFactory" /> <property name="dataSource" ref="dataSource" /> <property name="jpaDialect" ref="jpaDialect" /> </bean> <bean id="persistenceExceptionTranslationPostProcessor" class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor"/> </beans>

.Single(...)是一种扩展方法,但您尝试将其用作System.Linq上的静态方法。

如果要使用LINQ方法,则需要对实际的列表对象进行操作,而不是类LanguageList

即。 LanguageList.LanguageListItems

此外,由于它是一个静态类,因此LanguageList.LanguageListItems.Single(...)的继承无关紧要,可能会被删除。

答案 2 :(得分:1)

1)您不能使用.Single(),因为LanguageList是一个类,而不是实现IEnumerable&lt;的类的实例。 T>。如果你这样做,它应该有用:

new LanguageList().GetLanguageList.Single();

但实际上并没有这样做。 GetLanguageList没有理由成为实例成员;它应该是静态的。

2)你不应该公开语言列表,假设它是不可变的。

3)你应该使用Dictionary&lt; string,Word.WdLanguageID&gt;为您的语言表。它会更有效率,并且在构造期间强制执行唯一性,而不是在每次查找期间强制执行。

4)向LanguageList添加静态语言访问器(假设字典&lt;&gt;):

public static bool TryGetLanguageId(string name, out Word.WdLanguageID id)
{
    return _languageList.TryGetValue(name, out id);
}