8

そこで、次のユーティリティを使用して、クラスのインスタンスからフィールド/プロパティの名前を取得します...

public static string FieldName<T>(Expression<Func<T>> Source)
{
    return ((MemberExpression)Source.Body).Member.Name;
}

これにより、次のことが可能になります。

public class CoolCat
{
    public string KaratePower;
}

public class Program
{
    public static Main()
    {
        public CoolCat Jimmy = new CoolCat();

        string JimmysKaratePowerField = FieldName(() => Jimmy.KaratePower);
    }
}

これは、シリアル化や、フィールド名の文字列表現が必要な場合に最適です。

しかし今、クラスのインスタンスを持たずにフィールド名を取得できるようにしたい-たとえば、テーブルを設定していて、列のフィールド名をクラスの実際のフィールドに動的にリンクしたい場合(リファクタリングなどでは壊れません)。

基本的に、これを達成する方法の構文がよくわからないように感じますが、次のようになると思います。

public static string ClassFieldName<T>(Func<T> PropertyFunction)
{
     // Do something to get the field name?  I'm not sure whether 'Func' is the right thing here - but I would imagine that it is something where I could pass in a lambda type expression or something of the sort?
}

public class Program
{
    public static Main()
    {
        string CatsPowerFieldName = ClassFieldName<CoolCat>((x) => x.KaratePower);

        // This 'CatsPowerFieldName' would be set to "KaratePower".
    }
}

それが理にかなっていることを願っています-私はこの主題に関する語彙があまり得意ではないので、質問が少し曖昧であることを知っています.

4

3 に答える 3

4

あなたがやろうとしていることは、MicrosoftがSystem.Reflectionを作成した理由の1つです。これを試してください:

using System.Reflection;  // reflection namespace

public static List<Type> GetClassPropertyNames(Type myClass)
        {
            PropertyInfo[] propertyInfos;
            propertyInfos = myClass.GetProperties(BindingFlags.Public);

            List<Type> propertyTypeNames = new List<Type>();

            // write property names
            foreach (PropertyInfo propertyInfo in propertyInfos)
            {
                propertyTypeNames .Add(propertyInfo.PropertyType);
            }

            return propertyNames;

        }
于 2012-06-05T14:22:54.197 に答える
3

これを行うために使用する方法は2つあります。

1つ目は、任意のオブジェクトで使用できる拡張メソッドです。

public static string GetPropertyName<TEntity, TProperty>(this TEntity entity, Expression<Func<TEntity, TProperty>> propertyExpression)
{
    return propertyExpression.PropertyName();
}

のように使用されます

public CoolCat Jimmy = new CoolCat();
string JimmysKaratePowerField = Jimmy.GetPropertyName(j => j.KaratePower);

オブジェクトがないときに使用する2番目。

public static string PropertyName<T>(this Expression<Func<T, object>> propertyExpression)
{
        MemberExpression mbody = propertyExpression.Body as MemberExpression;

        if (mbody == null)
        {
            //This will handle Nullable<T> properties.
            UnaryExpression ubody = propertyExpression.Body as UnaryExpression;

            if (ubody != null)
            {
                mbody = ubody.Operand as MemberExpression;
            }

            if (mbody == null)
            {
                throw new ArgumentException("Expression is not a MemberExpression", "propertyExpression");
            }
        }

        return mbody.Member.Name;
}

これはそのように使用することができます

string KaratePowerField = Extensions.PropertyName<CoolCat>(j => j.KaratePower);
于 2012-06-05T17:41:02.937 に答える
2

ここでは、Reflectionを使用すると便利だと思います。現在VSを持っていませんが、typeof(class).GetMembers()のようなことができると確信しています。私の反射は少しさびています。

于 2012-06-05T14:23:26.783 に答える