2

反復したいconst文字列を使用してクラスのクラス(そのうちの1つを表示)を作成しました。

public static class HTDB_Cols
{
    public sealed class Assistant
    {
        public const string EntryID  = "entryID",
                CustName  = "custName",
                SerialNum  = "serialNum",
                UserName  = "userName",
                Password  = "password",
                EndDate  = "end_date",
                CustID  = "custID",
                TmpCheck  = "tmpCheck",
                Isfamily  = "isfamily",
                Isserver  = "isserver";
    }
}               

public static class DB
{    
    public static void insert(string TableName)
    {
        ColumnsCollection = typeof(HTDB_Cols).GetNestedTypes().Where(f => f.DeclaringType.Name.ToLower().Equals(TableName.ToLower()));
    } 
}

上記のコードは私の試みを示していますが、試行錯誤を繰り返した後でも、正しく理解できませんでした。

constコレクション配列またはリストとしてすべての列のリストが必要です。

4

2 に答える 2

5
var dict = typeof(HTDB_Cols).GetNestedTypes()
            .First(t=>String.Compare(t.Name,TableName,true)==0)
            .GetFields()
            .ToDictionary(f => f.Name, f => f.GetValue(null));

リストを取得するには

var list = typeof(HTDB_Cols).GetNestedTypes()
            .First(t => String.Compare(t.Name, TableName, true) == 0)
            .GetFields()
            .Select(f => f.GetValue(null) as string)
            .ToList();
于 2012-11-01T17:27:53.423 に答える
1

必要なものは次のようですenum

enum Assistant
{
    EntryID,
    CustName,
    SerialNum,
    UserName,
    Password,
    EndDate,
    CustID,
    TmpCheck,
    Isfamily,
    Isserver
};

次に、次のようにして、これらの名前をすべて文字列として取得できます。

string[] allNames = Enum.GetNames(typeof(Assistant));

変数の名前を気にする実際の値にすることが許容される限り、それは有効なオプションです。あなたの例ではそれらはまったく同じではありませんが、ほとんどはケーシングだけです。変数名を値として使用したり、変数名を必要な値に変更したりできる場合は、それが最善の選択肢である可能性があります。

ここで、変数名が表す値とは異なることが本当に重要な場合、または無効な識別子である値を表す必要がある場合 (たとえば、値の 1 つにスペースが含まれている場合、それは良くなく、数字で始めないでください。そうしないと、長すぎて便利な名前にならない可能性があります)。その場合、本当に必要なのは、整数やその他の数値型ではなく、文字列に基づく列挙型です。これは C# では厳密には可能ではありませんが、実際に次のクラスを作成する前にこれが発生したため、独自の文字列に基づく列挙型を作成するための最善の試みです。それらが表す文字列値とは異なる変数名が本当に必要な場合は、これでうまくいくはずです。

重要なものはすべて一番上にあり、その後のほとんどEqualsは単なるシンタックス シュガーです。

public struct StringEnum
{
    #region Code that is to be configured
    //For each value to be publicly exposed add a new field.
    public static readonly StringEnum Alpha = new StringEnum("Alpha Value");
    public static readonly StringEnum Beta = new StringEnum("Beta Value");
    public static readonly StringEnum Invalid = new StringEnum("Invalid");


    public static IEnumerable<StringEnum> AllValues
    {
        get
        {
            yield return Alpha;
            yield return Beta;
            yield return Invalid;
            //...
            //add a yield return for all instances here.

            //TODO refactor to use reflection so it doesn't need to be manually updated.
        }
    }

    #endregion
    private string value;

    /// <summary>
    /// default constructor
    /// </summary>
    //private Group()
    //{
    //    //You can make this default value whatever you want.  null is another option I considered 
    //    //(if this is a class an not a struct), but you 
    //    //shouldn't have this be anything that doesn't exist as one of the options defined at the top of 
    //    //the page.
    //    value = "Invalid";
    //}
    /// <summary>
    /// primary constructor
    /// </summary>
    /// <param name="value">The string value that this is a wrapper for</param>
    private StringEnum(string value)
    {
        this.value = value;
    }

    /// <summary>
    /// Compares the StringEnum to another StringEnum, or to a string value.
    /// </summary>
    /// <param name="obj"></param>
    /// <returns></returns>
    public override bool Equals(object obj)
    {
        if (obj is StringEnum)
        {
            return this.Equals((StringEnum)obj);
        }

        string otherString = obj as string;
        if (otherString != null)
        {
            return this.Equals(otherString);
        }

        throw new ArgumentException("obj is neither a StringEnum nor a String");
    }

    /// <summary>
    /// Strongly typed equals method.
    /// </summary>
    /// <param name="other">Another StringEnum to compare this object to.</param>
    /// <returns>True if the objects are equal.</returns>
    public bool Equals(StringEnum other)
    {
        return value == other.value;
    }

    /// <summary>
    /// Equals method typed to a string.
    /// </summary>
    /// <param name="other">A string to compare this object to.  
    /// There must be a Group associated with that string.</param>
    /// <returns>True if 'other' represents the same Group as 'this'.</returns>
    public bool Equals(string other)
    {
        return value == other;
    }

    /// <summary>
    /// Overridden equals operator, for convenience.
    /// </summary>
    /// <param name="first"></param>
    /// <param name="second"></param>
    /// <returns>True if the objects are equal.</returns>
    public static bool operator ==(StringEnum first, StringEnum second)
    {
        return object.Equals(first, second);
    }

    public static bool operator !=(StringEnum first, StringEnum second)
    {
        return !object.Equals(first, second);
    }

    /// <summary>
    /// Properly overrides GetHashCode so that it returns the hash of the wrapped string.
    /// </summary>
    /// <returns></returns>
    public override int GetHashCode()
    {
        return value.GetHashCode();
    }

    /// <summary>
    /// returns the internal string that this is a wrapper for.
    /// </summary>
    /// <param name="stringEnum"></param>
    /// <returns></returns>
    public static implicit operator string(StringEnum stringEnum)
    {
        return stringEnum.value;
    }

    /// <summary>
    /// Parses a string and returns an instance that corresponds to it.
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    public static StringEnum Parse(string input)
    {
        return AllValues.Where(item => item.value == input).FirstOrDefault();
    }

    /// <summary>
    /// Syntatic sugar for the Parse method.
    /// </summary>
    /// <param name="other"></param>
    /// <returns></returns>
    public static explicit operator StringEnum(string other)
    {
        return Parse(other);
    }

    /// <summary>
    /// A string representation of this object.
    /// </summary>
    /// <returns></returns>
    public override string ToString()
    {
        return value;
    }
}
于 2012-11-01T17:30:01.040 に答える