甲字符串表示的对象实例属性的可采取用Expression<Func<T>>:
string propertyName = ((MemberExpression) property.Body).Member.Name;
Run Code Online (Sandbox Code Playgroud)
但是,如果我没有(不想创建)实例呢?在这种情况下如何获取属性名称?
解释
我需要一个对象的属性名称的字符串表示.
假设有一个实体
public class Customer
{
public int ID;
public string Name;
}
Run Code Online (Sandbox Code Playgroud)
现在我想将这个实体的键表达式传递给其他函数,因此我需要字符串"ID",但我不想像字符串那样硬编码SomeOtherFunction("ID"),而是使用表达式SomeOtherFunction(ExpressionReader.GetString(() => CustomerInstance.ID)).为此,我需要提供实体实例.
现在我想在不创建实例的情况下做同样的事情.
我已经开始把自己列为"WPF陷阱"的列表:让我烦恼的事情,我不得不写下来记住,因为我每次都为他们堕落......
现在,我很确定你们在某一时刻偶然发现了类似的情况,我希望你们分享一下你们在这个问题上的经验:
什么是让你一直得到你的东西?你觉得最烦人的那个?
(我有几个问题似乎没有解释,也许你的提交将解释它们)
以下是我的一些"人物"陷阱(随机呈现):
即使单击位于控件(例如标签)的"透明"背景上而不仅仅是内容(本例中为文本)上,也要触发MouseEvent,控件的背景必须设置为"画笔".透明"而不仅仅是"null"(标签的默认值)
WPF DataGridCell的DataContext是单元所属的RowView,而不是CellView
在ScrollViewer中,滚动条由滚动查看器本身管理(即设置属性,如ScrollBar.Value没有效果)
Key.F10当您按下"F10",而不是你是不是开除Key.System,你必须去看看e.SystemKey,以获得Key.F10
......现在你开始了.
有没有办法检查ASP.Net MVC 5中的particualr字段是否存在验证消息.我需要以Razaor形式检查这个
目前是IsNullOrEmpty,但我认为ValidationMessage确实会返回一些html标签,即使没有验证错误?
我需要在验证消息存在时才显示div,即对于特定字段的验证失败.
使用UrlHelperMVC3,您可以使用字符串为控制器和操作构建URL:
@Url.Action("Index", "Somewhere")
Run Code Online (Sandbox Code Playgroud)
哪个会将请求路由到SomewhereController.Index().
我想做的是获取控制器和操作的URL,但是传递控制器的类型:
@Url.Action("Index", typeof(SomewhereController))
Run Code Online (Sandbox Code Playgroud)
有没有办法做到这一点?
编辑/澄清:
我意识到控制器的约定是控制器名称路由到一个名为的类,{Name}Controller所以我可以从我的Type.Name的末尾删除'Controller'.我想我假设有一种方法可以通过一些自定义路由覆盖此约定.虽然我看得越多,但我不确定是否可能......
也许MVC3 只能路由到名为"*Controller"的类?我正在通过MVC3源寻找"控制器"硬编码的地方,但还没有找到答案......但是如果可以将"Somewhere"路由到类SomewhereFoo而不是SomewhereController,那么只需删除"Controller"从类名称将是不正确的.
如果某人可以向我提供证据支持或反对"控制器"被硬编码到某个地方的MVC3,那么我会对"从名称中移除控制器"的方法感到更舒服.
有没有办法如何从Lambda表达式获取名称ov事件,如与属性( 从lambda表达式检索属性名称)?
谢谢
我的项目中有一些单元测试,我们希望能够设置一些具有私有设置器的属性.目前我正在通过反射和这种扩展方法来做到这一点:
public static void SetPrivateProperty(this object sourceObject, string propertyName, object propertyValue)
{
sourceObject.GetType().GetProperty(propertyName).SetValue(sourceObject, propertyValue, null);
}
Run Code Online (Sandbox Code Playgroud)
假设我有一个像这样的TestObject:
public class TestObject
{
public int TestProperty{ get; private set; }
}
Run Code Online (Sandbox Code Playgroud)
然后我可以在我的单元测试中调用它,如下所示:
myTestObject.SetPrivateProperty("TestProperty", 1);
Run Code Online (Sandbox Code Playgroud)
但是,我想在编译时验证属性名称,因此我希望能够在via表达式中传递属性,如下所示:
myTestObject.SetPrivateProperty(o => o.TestProperty, 1);
Run Code Online (Sandbox Code Playgroud)
我怎样才能做到这一点?
我有一个方法,我想转换为扩展方法
public static string GetMemberName<T>(Expression<Func<T>> item)
{
return ((MemberExpression)item.Body).Member.Name;
}
Run Code Online (Sandbox Code Playgroud)
并称之为
string str = myclass.GetMemberName(() => new Foo().Bar);
Run Code Online (Sandbox Code Playgroud)
所以评估为 str = "Bar"; // It gives the Member name and not its value
现在,当我尝试将此转换为扩展方法时
public static string GetMemberName<T>(this Expression<Func<T>> item)
{
return ((MemberExpression)item.Body).Member.Name;
}
Run Code Online (Sandbox Code Playgroud)
并称之为
string str = (() => new Foo().Bar).GetMemberName();
Run Code Online (Sandbox Code Playgroud)
错误说 Operator '.' cannot be applied to operand of type 'lambda expression'
我哪里错了?
我想强输入属性名称
myMethod(model => model.userId);
public myMethod(model: () => any) {
//Must print "userId"
}
Run Code Online (Sandbox Code Playgroud)
我已经知道这行不通,因为 JavaScript 会评估 userId。
在 C# 中很容易做到:
是否可以在 TypeScript/JavaScript 中做到这一点?
//ModelFor(person =>person.Name);
public void ModelFor<TModel, TValue>(
Expression<Func<TModel, TValue>> expression)
{
//Result should be "Name"
string nameOfTValue = ????;
}
Run Code Online (Sandbox Code Playgroud) 我想在C#中模仿F#'with'关键字(可以在记录中使用).
现在,当我创建一个新的不可变类时,我只需手动添加一些自定义的"with"方法,如下所示:
public class MyClass
{
public readonly string Name;
public readonly string Description;
public MyClass(string name, string description)
{
this.Name = name;
this.Description = description;
}
// Custom with methods
public MyClass WithName(string name)
{
return new MyClass(name, this.Description);
}
public MyClass WithDescription(string description)
{
return new MyClass(this.Name, description);
}
}
Run Code Online (Sandbox Code Playgroud)
对于我个人的c#开发,我试图创建一个通用的方法来做到这一点(在完美的世界中,我会使用F#).我做的"最好的"是这样的:
public static class MyExtensions
{
public static TSource With<TSource, TField>(
this TSource obj,
string fieldName,
TField value)
where TSource : class
{
// Reflection stuff to use …Run Code Online (Sandbox Code Playgroud) c# ×8
.net ×4
lambda ×4
asp.net ×1
asp.net-mvc ×1
immutability ×1
javascript ×1
linq ×1
properties ×1
reflection ×1
typescript ×1
validation ×1
wpf ×1