Accessing C# property name or attributes

前端 未结 7 2127
花落未央
花落未央 2021-01-31 21:39

I would like to automatically generate SQL statements from a class instance. The method should look like Update(object[] Properties, object PrimaryKeyProperty). The method is pa

相关标签:
7条回答
  • 2021-01-31 21:47

    Let's say (from the first sample, method update of a class MyClass):

    public class MyClass {
    
    public int iMyStatusProperty { get; set; }
    public int iMyKey { get; set; }
    
    public int UpdateStatusProperty(int iValue){
    this.iMyStatusProperty = iValue;
    return _Update( new[iMyStatusProperty ], iMyKey); // this should generate SQL: "UPDATE MyClass set iMyStatusProperty = {iMyStatusProperty} where iMyKey = {iMyKey}"
    }
    

    {iMyStatusProperty} and {iMyKey} are property values of a class instance.

    So, the problem is how to get property name (reflection) from a property without using names of properties as strings (to avoid field name typos).

    0 讨论(0)
  • 2021-01-31 21:54

    You can do something like this:

    Type t = someInstance.getType();
    
    foreach (MemberInfo mi in t.GetMembers())
    {
        if (mi.MemberType == MemberTypes.Property)
        {
            Console.WriteLine(mi.Name);
        }
    }
    

    to get all the property names for instance's type.

    0 讨论(0)
  • 2021-01-31 21:55

    I found a perfect solution in This Post

    public static string GetPropertyName<T>(Expression<Func<T>> propertyExpression)
    {
       return (propertyExpression.Body as MemberExpression).Member.Name;
    }
    

    And then for the usage :

    var propertyName = GetPropertyName(
    () => myObject.AProperty); // returns "AProperty"
    

    Works like a charm

    0 讨论(0)
  • 2021-01-31 21:55

    Not 100% sure if this will get you what you're looking for, this will fetch all properties with [Column] attribute inside your class: In the datacontext I have:

     public ReadOnlyCollection<MetaDataMember> ColumnNames<TEntity>( )
        {
            return this.Mapping.MappingSource.GetModel(typeof(DataContext)).GetMetaType(typeof(TEntity)).DataMembers;
        }
    

    Fetching the table column-names that are properties inside the class:

           MyDataContext db = GetDataContext(); 
           var allColumnPropertyNames =   db.ColumnNames<Animal>().Where(n => n.Member.GetCustomAttributes(typeof(System.Data.Linq.Mapping.ColumnAttribute), false).FirstOrDefault() != null).Select(n => n.Name);
    
    0 讨论(0)
  • 2021-01-31 21:58

    Just wrote an implementation of this for a presentation on lambdas for our usergroup last Tuesday.

    • You can do

      MembersOf<Animal>.GetName(x => x.Status)

    • Or

      var a = new Animal() a.MemberName(x => x.Status)

    the code:

    public static class MembersOf<T> {
        public static string GetName<R>(Expression<Func<T,R>> expr) {
            var node = expr.Body as MemberExpression;
            if (object.ReferenceEquals(null, node)) 
                throw new InvalidOperationException("Expression must be of member access");
            return node.Member.Name;
        }
    }
    
    • Link to the presentation and code samples.
    • Also in SVN (more likely to be updated): http://gim-projects.googlecode.com/svn/presentations/CantDanceTheLambda
    0 讨论(0)
  • 2021-01-31 22:00

    Since you already have an explicit handle to the specific property you want, you know the name - can you just type it?

    0 讨论(0)
提交回复
热议问题