2010-12-02 2 views
1

私のウェブサイトのすべての文字列を含む.resxファイルがあります。私はこのようなそれぞれの新しい文字列のadd()を使用せずにこれらの文字列のサブセットのList<string>を作成したい:私は使用することができ.RESXファイル内のキーと文字列のサブセットのリスト<>を作成する

List<string> listOfResourceStrings= new List<string>(); 
listOfResourceStrings.Add(Resources.WebSite_String1); 
listOfResourceStrings.Add(Resources.WebSite_String2); 
listOfResourceStrings.Add(Resources.WebSite_String3); 
listOfResourceStrings.Add(Resources.WebSite_String4); 
listOfResourceStrings.Add(Resources.WebSite_String5); 
listOfResourceStrings.Add(Resources.WebSite_Stringn); 

...

System.Resources.ResourceSet listOfResourceStrings = Resources.ResourceManager.GetResourceSet(System.Threading.Thread.CurrentThread.CurrentCulture, true, true); 

...しかし、これはResourceSetを返し、すべての文字列を含みます。そして、文字列の部分集合を見つけるのは簡単な方法ではないようです。

はあなたの助けをありがとう、

アーロン

+0

あなたが望むサブセットを知っていますか、それとも動的に決定していますか? – seekerOfKnowledge

+0

私は 'find(" Website _ ")やそのようなことを言っています。だから、動的に。 –

答えて

0

リソースデータは、静的プロパティに格納されているので、反射が選択プロパティ名に基づいてプロパティ値を抽出するために使用することができます。

using System; 
using System.Collections.Generic; 
using System.Reflection; 
using System.Text.RegularExpressions; 

namespace ConsoleApplication9 
{ 
    public class Program 
    { 
    public static void Main(String[] args) 
    { 
     var strings = GetStringPropertyValuesFromType(typeof(Properties.Resources), @"^website_.*"); 
     foreach (var s in strings) 
     Console.WriteLine(s); 
    } 

    public static List<String> GetStringPropertyValuesFromType(Type type, String propertyNameMask) 
    { 
     var result = new List<String>(); 
     var propertyInfos = type.GetProperties(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static); 
     var regex = new Regex(propertyNameMask, RegexOptions.IgnoreCase | RegexOptions.Singleline); 

     foreach (var propertyInfo in propertyInfos) 
     { 
     if (propertyInfo.CanRead && 
      (propertyInfo.PropertyType == typeof(String)) && 
      regex.IsMatch(propertyInfo.Name)) 
     { 
      result.Add(propertyInfo.GetValue(type, null).ToString()); 
     } 
     } 

     return result; 
    } 
    } 
} 
関連する問題