相关疑难解决方法(0)

使用lambda表达式获取属性名称和类型

我正在尝试编写一个函数,它将使用如下语法提取属性的名称和类型:

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"

有人可以帮忙吗?

c# reflection lambda

37
推荐指数
1
解决办法
3万
查看次数

如何创建一个调用IEnumerable <TSource> .Any(...)的表达式树?

我正在尝试创建一个表示以下内容的表达式树:

myObject.childObjectCollection.Any(i => i.Name == "name");
Run Code Online (Sandbox Code Playgroud)

为清楚起见,我有以下内容:

//'myObject.childObjectCollection' is represented here by 'propertyExp'
//'i => i.Name == "name"' is represented here by 'predicateExp'
//but I am struggling with the Any() method reference - if I make the parent method
//non-generic Expression.Call() fails but, as per below, if i use <T> the 
//MethodInfo object is always null - I can't get a reference to it

private static MethodCallExpression GetAnyExpression<T>(MemberExpression propertyExp, Expression predicateExp)
{
    MethodInfo method = typeof(Enumerable).GetMethod("Any", new[]{ typeof(Func<IEnumerable<T>, Boolean>)});
    return …
Run Code Online (Sandbox Code Playgroud)

.net c# linq expression-trees

37
推荐指数
2
解决办法
1万
查看次数

使用Lambda表达式获取方法名称

我正在尝试使用lambda表达式获取类型的方法名称.我正在使用Windows Identity Foundation,需要定义具有类型名称的访问策略,其中命名空间作为资源,方法名称作为操作.这是一个例子.

这是我将从以下类型获取类型名称和方法名称的类型:

namespace My.OrderEntry {
    public class Order {
        public void AddItem(string itemNumber, int quantity) {}
    }
}
Run Code Online (Sandbox Code Playgroud)

这就是我想通过DSL定义访问策略的方法:

ForResource<Order>().Performing(o => o.AddItem).AllowUsersHaving(new Claim());
Run Code Online (Sandbox Code Playgroud)

从该声明中,我想将"My.OrderEntry.Order"作为资源,将"AddItem"作为动作.获取带有命名空间的类型名称是没有问题的,但我认为我不能将lambda用于我正在尝试的方法.

public static IPermissionExp Performing<T>(
    this IActionExp<T> exp,
    Func<T, delegate???> action) {} //this is where I don't know what to define
Run Code Online (Sandbox Code Playgroud)

这种事情甚至可能吗?有没有其他方法可以在不使用魔术字符串的情况下完成此类操作?

c# reflection lambda

17
推荐指数
1
解决办法
7300
查看次数

将参数替换为指向lambda表达式中的嵌套参数

由于前一个问题的一些答案,我可以成功地替换lambda表达式中的简单参数类型,但我无法弄清楚如何将传入的lambda中的参数替换为嵌套参数.

考虑以下对象:

public class DtoColour {

    public DtoColour(string name)
    {
        Name = name;
    }

    public string Name { get; set; }

    public ICollection<DtoFavouriteColour> FavouriteColours { get; set; }
}

public class DtoPerson
{
    public DtoPerson(string firstName, string lastName)
    {
        FirstName = firstName;
        LastName = lastName;
        FavouriteColours = new Collection<DtoFavouriteColour>();
    }

    public string FirstName { get; private set; }

    public string LastName { get; private set; }

    public ICollection<DtoFavouriteColour> FavouriteColours { get; set; }
}

public class DtoFavouriteColour
{ …
Run Code Online (Sandbox Code Playgroud)

c# lambda expression visitor

8
推荐指数
2
解决办法
925
查看次数

标签 统计

c# ×4

lambda ×3

reflection ×2

.net ×1

expression ×1

expression-trees ×1

linq ×1

visitor ×1