标签: custom-attributes

类成员的自定义属性

我正在使用自定义属性来定义类的成员如何映射到属性以作为表单发布(支付网关)发布.我有自定义属性工作正常,并能够通过"名称"获取属性,但希望成员本身获取属性.

例如:

getFieldName("name");
Run Code Online (Sandbox Code Playgroud)

VS

getFieldName(obj.Name);
Run Code Online (Sandbox Code Playgroud)

计划是编写一个方法,将带有成员的类序列化为一个postable字符串.

这是我此时的测试代码,其中ret是一个字符串,PropertyMapping是自定义属性:

foreach (MemberInfo i in (typeof(CustomClass)).GetMember("Name"))
{
    foreach (object at in i.GetCustomAttributes(true))
    {
        PropertyMapping map = at as PropertyMapping;
        if (map != null)
        {
            ret += map.FieldName;
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

提前致谢!

c# reflection attributes custom-attributes

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

真实世界使用自定义.NET属性

在现实世界中,您使用自定义.NET属性的是什么类型的东西?

我已经阅读了几篇关于它们的文章,但我从未使用过自定义属性.

我觉得当它们有用时我可能会忽略它们.

我在谈论您创建的属性,而不是已经包含在框架中的属性.

.net attributes custom-attributes class-attributes .net-attributes

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

将属性限制为类或属性是否可行?

我有两个自定义属性定义如下:

internal class SchemaAttribute : Attribute {
    internal SchemaAttribute(string schema) {
        Schema = schema;
    }

    internal string Schema { get; private set; }
}

internal class AttributeAttribute : Attribute {
    internal AttributeAttribute(string attribute) {
        Attribute = attribute;
    }

    internal string Attribute { get; private set; }
}
Run Code Online (Sandbox Code Playgroud)

我想将SchemaAttribute限制为类,将AttributeAttribute限制为属性.

这可行吗?

c# vb.net attributes custom-attributes .net-2.0

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

在asp.net mvc中的HttpUnauthorizedResult上的默认登录URL

AuthorizeAttribute在asp.net mvc3应用程序中编写了一个具有以下条件的自定义:

public override void OnAuthorization(AuthorizationContext filterContext)
{     
    //auth failed, redirect to Sign In
    if (!filterContext.HttpContext.User.Identity.IsAuthenticated)
    {
       filterContext.Result = new HttpUnauthorizedResult();
    }
}
Run Code Online (Sandbox Code Playgroud)

在我的web.config中,我有:

<authentication mode="Forms">
  <forms loginUrl="~/User/SignIn" timeout="2880" />
</authentication>
Run Code Online (Sandbox Code Playgroud)

验证失败后,默认情况下会重定向到"/帐户/登录"页面.

如何更改此默认重定向网址并将其重定向到"/ User/SignIn"?

屏幕截图显示了我想说的内容的清晰视图.HttpUnauthorizedresult图像

虽然我设置了'/ User/SignIn',但它会重定向到'/ Account/Login'

authentication asp.net-mvc forms-authentication custom-attributes

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

你可以将属性应用于C#中的多个字段吗?

这似乎不太可能,但无论如何我都会问......在C#中是否可以同时将单个属性应用于多个字段?

public class MyClass {
     [SomeAttribute]
     public int m_nVar1;
     [SomeAttribute]
     public int m_nVar2;
     public int m_nVar3;
}
Run Code Online (Sandbox Code Playgroud)

是否有一个简单的方法将"SomeAttribute"放在m_Var1和m_Var2上,而不是放在m_nVar3上?目前,我们将属性放在每个字段之前,但是将所有字段放在块中使用属性会很好.

c# custom-attributes

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

如何在授权过滤器MVC 4之前执行操作过滤器

我通过继承自AuthorizeAttribute类在MVC 4中实现了自己的自定义授权属性.我也有一个习惯ActionFilterAttribute.这些都很好,但问题在于订购它们.我需要自定义Action Filter在自定义Authorize Filter之前运行.

我已尝试将Order属性用于属性,但据我了解,授权过滤器将始终在动作过滤器之前运行.

有谁知道如何在授权过滤器之前强制执行A​​ction Filter?

custom-attributes asp.net-mvc-4

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

如何检查方法是否具有属性(带接口,强制转换和抽象)

在阅读了文章" 如何检查方法是否具有属性 "之后,我是解决让我保持清醒的问题的一步.

我介绍一下情况:

(我正在使用ASP.Net MVC 4)

这些接口:

public interface IFlyable
{
    ActionResult Fly();
}    

public interface IRunnable
{
    ActionResult Run();
}
Run Code Online (Sandbox Code Playgroud)

这个抽象类:

public abstract class SuperHero : Controller
{
    public void SavePeople()
    {
    }    
}
Run Code Online (Sandbox Code Playgroud)

这个控制器:

public class SuperManController : SuperHero,IFlyable,IRunnable {

    [Authorize]
    public ActionResult Fly(){
        // Flying...
    }    

    [Authorize]
    public ActionResult Run(){
        // Running...
    }    

}
Run Code Online (Sandbox Code Playgroud)

这个抽象类(用于测试)

[TestClass]
public abstract class SuperHeroTest<TSuperHero>{

    protected abstract TSuperHero GetSuperHero();

    [TestMethod]
    public void IfSuperHeroCanFlyMustHaveAuthorizeAttribute(){

        var superHero=GetSuperHero();

        if(superHero is IFlyable)
        { …
Run Code Online (Sandbox Code Playgroud)

c# reflection asp.net-mvc unit-testing custom-attributes

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

自定义与非自定义属性?

实现ICustomAttributeProvider接口的东西将允许您获取已通过该GetCustomAttributes方法应用于它的自定义属性.据我所知,自定义属性基本上是一个特殊的类(以"属性"结尾并扩展Attribute类),它是使用适当的语法([FooAttribute]在方法/类/等之前)应用于类似方法或类的类.例如,在C#中.但如果这是一个自定义属性,那么什么是非自定义属性?我曾经认为与.NET捆绑在一起的属性是非自定义的,但是GetCustomAttributes甚至会返回我的属性System.ThreadStaticAttribute,这些属性是.NET框架的核心.

是否存在非自定义属性,或者"自定义属性"只是一个重言式?

.net c# custom-attributes

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

Web API中的自定义IAuthenticationFilter和AllowAnonymous

我想利用AllowAnonymous和习惯AuthenticationFilter.有人能指出我正确的方向使用AllowAnonymous或另一种选择吗?谢谢

我创建了自己的自定义过滤器,它继承自System.Attribute并实现System.Web.Http.Filters.IAuthenticationFilter

 public class MyCustomAuthenticationAttribute : Attribute, IAuthenticationFilter
Run Code Online (Sandbox Code Playgroud)

我已经能够成功添加该AuthenticateAsync方法的逻辑

 public async Task AuthenticateAsync(
     HttpAuthenticationContext context, 
     CancellationToken cancellationToken) {}
Run Code Online (Sandbox Code Playgroud)

我的问题是我需要忽略一些Web API控制器操作或控制器.我以为我可以System.Web.Http.AllowAnonymousAttribute用来做这个.例如,这是一个显示意图的非常简单的示例.

[MyCustomAuthentication]
public class HomeController : ApiController
{
    // no authentication needed allow anonymous 
    [HttpGet]
    [Route("hianonymous")]
    [AllowAnonymous]
    public IHttpActionResult Hello(string name) {
        return Ok(new { message = "hello " + name }); 
    }

    // needs to be authenticated 
    [HttpGet] 
    [Route("hiauthenticated")]
    public IHttpActionResult Hello() {
        var name = User.Identity.Name; …
Run Code Online (Sandbox Code Playgroud)

c# authentication custom-attributes asp.net-web-api2

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

忽略/覆盖AttributeUsage限制

我需要能够应用于DisplayAttribute类,但它AttributeUsage不允许在当前的.NET/.NET Core版本中.看起来这已经针对.NET Core vNext进行了补救,但如果有一些解决方法能够以某种方式忽略或覆盖此限制,直到此更改进入.NET版本非常有用.我能看到的唯一选择是重新实现整个事情(包括本地化),但我真的不想支持和测试,只要在.NET vNext发布时就弃用它.

任何聪明的想法/黑客?

在CLR运行时是否验证了AttributeUsage限制,还是只是编译时间限制?如果它们只是编译时间检查,那么是否有一种聪明的方法来更改编译器使用的元数据"欺骗"它允许使用或以某种方式修改系统程序集,以便我的开发机器允许使用?

*我似乎无法编辑赏金说明,只是为了澄清,赏金的解决方案必须适用于.NET Framework,以及.NET Core的奖励积分.

.net c# custom-attributes .net-core

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