相关疑难解决方法(0)

为什么C#禁止通用属性类型?

这会导致编译时异常:

public sealed class ValidatesAttribute<T> : Attribute
{

}

[Validates<string>]
public static class StringValidation
{

}
Run Code Online (Sandbox Code Playgroud)

我意识到C#不支持通用属性.然而,经过大量的谷歌搜索,我似乎无法找到原因.

有谁知道为什么泛型类型无法衍生出来Attribute?任何理论?

c# generics .net-attributes

494
推荐指数
7
解决办法
7万
查看次数

AutoMapper Map子属性,也定义了地图

我有以下域对象:

public class DomainClass
{
    public int Id { get; set; }

    public string A { get; set; }
    public string B { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

我有以下两个要映射到的对象:

public class Parent 
{
    public int Id { get; set; }
    public string A { get; set; }

    public Child Child { get; set; }
}

public class Child 
{
    public int Id { get; set; }
    public string B { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

我设置了以下地图:

 Mapper.CreateMap<DomainClass, Parent>();
 Mapper.CreateMap<DomainClass, Child>();
Run Code Online (Sandbox Code Playgroud)

如果我使用以下调用映射我的对象,则parent.Child属性为null.

var …
Run Code Online (Sandbox Code Playgroud)

c# automapper

52
推荐指数
3
解决办法
5万
查看次数

授权属性生命周期

有人可以解释为什么授权属性生命周期似乎是相对于它应用的类或方法进行管理的?这与相对于请求生命周期进行管理相反.

如果我在类级别修饰控制器,则只有在对同一个控制器的多个请求中调用一次授权属性构造函数.如果我装饰每个控制器方法,那么我会为每个调用的控制器方法获取新的authorize属性构造函数调用.

什么是这种行为?我希望每个请求都能创建授权属性.

asp.net-mvc authorize-attribute

8
推荐指数
1
解决办法
1782
查看次数

ASP.NET MVC 3,动作过滤器和Autofac依赖注入

ASP.NET MVC 2上,我有一个ActionFilterAttribute调用[Transaction],在执行操作之前启动一个NHibernate事务,然后提交或回滚它,具体取决于是否抛出异常.该ISession实例HttpRequestScoped()Autofac注入.它看起来像这样,效果很好:

[AttributeUsage(AttributeTargets.Method | AttributeTargets.Class)]
public sealed class TransactionAttribute : ActionFilterAttribute
{
    private ITransaction transaction;

    public TransactionAttribute()
    {
        this.Order = 0;
    }

    public ISession Session
    {
        get;
        set;
    }

    public override void OnActionExecuted(
        ActionExecutedContext filterContext)
    {
        if (this.Session != null && this.transaction != null)
        {
            try
            {
                if (this.transaction.IsActive)
                {
                    if (filterContext.Exception == null)
                    {
                        this.transaction.Commit();
                    }
                    else
                    {
                        this.transaction.Rollback();
                    }
                }
            }
            finally
            {
                this.transaction.Dispose();
                this.transaction …
Run Code Online (Sandbox Code Playgroud)

autofac action-filter asp.net-mvc-3

6
推荐指数
1
解决办法
5630
查看次数