相关疑难解决方法(0)

获取属性的名称作为字符串

(参见下面我使用我接受的答案创建的解决方案)

我正在尝试提高一些涉及反射的代码的可维护性.该应用程序有一个.NET Remoting接口,公开(除此之外)一个名为Execute的方法,用于访问未包含在其已发布的远程接口中的应用程序部分.

以下是应用程序如何指定可通过Execute访问的属性(本例中为静态属性):

RemoteMgr.ExposeProperty("SomeSecret", typeof(SomeClass), "SomeProperty");
Run Code Online (Sandbox Code Playgroud)

所以远程用户可以调用:

string response = remoteObject.Execute("SomeSecret");
Run Code Online (Sandbox Code Playgroud)

并且应用程序将使用反射来查找SomeClass.SomeProperty并将其值作为字符串返回.

不幸的是,如果有人重命名SomeProperty并忘记更改ExposeProperty()的第3个parm,它会破坏这种机制.

我需要相当于:

SomeClass.SomeProperty.GetTheNameOfThisPropertyAsAString()
Run Code Online (Sandbox Code Playgroud)

在ExposeProperty中用作第三个parm,因此重构工具将负责重命名.

有没有办法做到这一点?提前致谢.

好的,这是我最终创建的内容(根据我选择的答案和他引用的问题):

// <summary>
// Get the name of a static or instance property from a property access lambda.
// </summary>
// <typeparam name="T">Type of the property</typeparam>
// <param name="propertyLambda">lambda expression of the form: '() => Class.Property' or '() => object.Property'</param>
// <returns>The name of the property</returns>
public string GetPropertyName<T>(Expression<Func<T>> propertyLambda)
{
    var me = propertyLambda.Body as MemberExpression;

    if (me == null)
    { …
Run Code Online (Sandbox Code Playgroud)

c# reflection properties

191
推荐指数
9
解决办法
20万
查看次数

数据绑定POCO属性

是否有任何数据绑定允许之间的绑定框架(BCL或其他方式)的任何两个CLR性能实现INotifyPropertyChangedINotifyCollectionChanged?似乎应该可以做这样的事情:

var binding = new Binding();
binding.Source = someSourceObject;
binding.SourcePath = "Customer.Name";
binding.Target = someTargetObject;
binding.TargetPath = "Client.Name";
BindingManager.Bind(binding);
Run Code Online (Sandbox Code Playgroud)

在哪里someSourceObjectsomeTargetObject只是实施的POCO INotifyPropertyChanged.但是,我没有意识到BCL对此有任何支持,并且不确定是否存在允许这样做的现有框架.

更新:鉴于没有现有的库,我已经自己写了自己的库.它可以在这里找到.

谢谢

.net c# data-binding poco system.componentmodel

17
推荐指数
2
解决办法
4177
查看次数

如何将此方法作为扩展方法添加到我的类的属性中?

我有一个方法,我想将此方法作为扩展方法添加到我的类的属性.此方法将表达式作为输入参数.方法如下:

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

我想像下面的例子一样使用这个方法:

string propertyName = MyClass.Property1.GetPropertyName();
Run Code Online (Sandbox Code Playgroud)

可能吗?如果是的话,解决方案是什么?

c# extension-methods expression properties

4
推荐指数
1
解决办法
1725
查看次数

获取属性名称的扩展方法

我有一个扩展方法来获取属性名称

public static string Name<T>(this Expression<Func<T>> expression)
{
    MemberExpression body = (MemberExpression)expression.Body;
    return body.Member.Name;
}
Run Code Online (Sandbox Code Playgroud)

我称之为

string Name = ((Expression<Func<DateTime>>)(() => this.PublishDateTime)).Name();
Run Code Online (Sandbox Code Playgroud)

这工作正常,并将我PublishDateTime作为字符串返回.

但是我对调用语句有一个问题,它看起来太复杂了,我想要这样的东西.

this.PublishDateTime.Name()
Run Code Online (Sandbox Code Playgroud)

有人可以修改我的扩展方法吗?

c# reflection extension-methods propertyinfo

4
推荐指数
2
解决办法
4596
查看次数

获取扩展方法的被调用者名称

我构建了一个简单的ArgumentValidator类,以简化任何给定方法中的参数前提条件。其中大多数都是null或边界检查,经过几次之后就会变得非常乏味

if (arg == null ) throw new ArgumentNullException(nameof(arg));
Run Code Online (Sandbox Code Playgroud)

所以我提出了以下设置:

public static class ArgumentValidator
{
    public interface IArgument<T>
    {
        string ParamName { get; }
        T Value { get; }
    }

    private class Argument<T>: IArgument<T>
    {
        public Argument(T argument, string paramName)
        {
            ParamName = paramName;
            Value = argument;
        }

        public string ParamName { get; }
        public T Value { get; }
    }

    public static IArgument<T> Validate<T>(this T argument, string paramName = null)
    {
        return new Argument<T>(argument, paramName …
Run Code Online (Sandbox Code Playgroud)

c#

4
推荐指数
1
解决办法
1602
查看次数