我正在尝试编写一个函数,它将使用如下语法提取属性的名称和类型:
private class SomeClass
{
Public string Col1;
}
PropertyMapper<Somewhere> propertyMapper = new PropertyMapper<Somewhere>();
propertyMapper.MapProperty(x => x.Col1)
Run Code Online (Sandbox Code Playgroud)
有没有办法将属性传递给函数而不对此语法进行任何重大更改?
我想获取属性名称和属性类型.
所以在下面的例子中我想要检索
Name = "Col1" 和 Type = "System.String"
有人可以帮忙吗?
"nameof"表达式在Visual Studio 2015和c#6中引入
如何在旧版本中使用它或编写类似的方法,如.net framework 4.
我想从类实例中自动生成SQL语句.该方法应该看起来像Update(object []属性,对象PrimaryKeyProperty).该方法是实例的一部分(类,基本方法 - 任何子代的通用).属性数组是类属性的数组,将在update语句中使用.属性名称等于表字段名称.
问题是我无法获得属性名称.
有没有选项在类实例中获取属性名称?样品:
public class MyClass {
public int iMyProperty { get; set; }
public string cMyProperty2 { get; set; }
{
main() {
MyClass _main = new MyClass();
_main.iMyProperty.*PropertyName* // should return string "iMyProperty"
{
Run Code Online (Sandbox Code Playgroud)
我知道PropertyInfo,但我不知道从GetProperties()数组中获取属性的ID.
有什么建议吗?
只花了几个小时拉我的头发试图弄清楚为什么我的ssis Script Component没有在遇到断点时进入调试器.我搜索了网络并资助64位设置(项目 - >专家 - >调试)关闭,但它没有帮助我.
事实证明,如果我在代码中使用字符串插值($"{someVar}"),则调试器无法启动.一旦我用旧的string.Format("{0} ...",param1,...)方法替换它,我的断点就被击中了,我可以逐步完成代码.
代码以任何一种方式工作,它只是受较新语法影响的调试器.
我希望这可以帮助别人.
我有这样的静态方法:
public static string MyMethod(Func<Student, object> func)
{
return ??? ;
}
Run Code Online (Sandbox Code Playgroud)
我用它如下:
var s1 = MyMethod(student => student.ID); // Return "ID" ???
var s2 = MyMethod(student => student.Age); // Return "Age" ???
var s3 = MyMethod(student => student.Name); // Return "Name" ???
Run Code Online (Sandbox Code Playgroud)
怎么写方法返回以下结果?
*在=>之后返回每个属性的名称作为字符串
c# ×5
c#-4.0 ×1
c#-6.0 ×1
delegates ×1
func ×1
lambda ×1
nameof ×1
properties ×1
reflection ×1
ssis ×1