标签: custom-attributes

你认为C#属性(或类似的机制)是个好主意还是你不鼓励使用它们?

我基于过去4或5年使用C#属性的许多设计和框架.

但最近我看到许多人开始不鼓励他们使用或改变他们的框架以减少他们的需要或使用.

我发现它们是天赐之物,但现在我开始怀疑自己错过了什么.

澄清:使用约定优于配置正成为遵循ORM领域的主要原则.此区域是否可以使用配置文件(XML)映射字段,使用属性或具有直接映射到数据库表中字段的通用命名约定.我没有引用任何引用,但我已经阅读了一些反对添加另一个属性的反对意见.

但我觉得在我刚刚列出的三个选项中,属性仍然是最有意义的.Config文件难以维护,常见的命名约定将您与数据库字段的实现联系起来.属性被准确放置在需要它们的位置,并且实现可以在不断开其使用位置的情况下进行更改.

c# attributes custom-attributes

3
推荐指数
2
解决办法
1213
查看次数

使用属性进行方法分析

是否可以通过.NET中的属性分析各个方法?

我目前正在尝试在大量遗留应用程序中找到一些瓶颈,这些应用程序大量使用静态方法.目前,集成框架根本不是一种选择.由于大多数调用都使用静态方法,因此接口和依赖注入不可用.攻击代码以记录诊断也不是一个可行的解决方案.

我知道市场上有一些分析工具,但它们目前不在预算范围内.理想情况下,我将能够创建自己的自定义属性,该属性将记录有关方法输入和方法退出的一些基本信息.我从来没有真正使用自定义属性,所以任何洞察甚至是否可能这将是值得赞赏的.

如果可能的话,我想通过配置文件启用分析.这将通过单元和集成测试支持分析.

.net profiling custom-attributes

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

Objective-C:自定义BOOL访问器(getter和setter)方法

我知道有人已经问过为BOOL变量编写getter和setter.但是,如果我定义自定义的getter和setter方法setImmediateisImmediate分别,我想passcode.immediate = NO工作过.

我没有任何实例变量,但也许我应该?我可以添加一个NSDate *lastUnlocked.

到目前为止,这是相关的代码:

// PasscodeLock.h

extern NSString *const kPasscodeLastUnlocked;

@interface PasscodeLock : NSObject {

}

- (BOOL)isImmediate;
- (void)setImmediate:(BOOL)on;

- (NSDate *)lastUnlocked;
- (void)resetLastUnlocked;
- (void)setLastUnlocked:(NSDate *)lastUnlocked;

@end


// PasscodeLock.m

#import "PasscodeLock.h"

NSString *const kPasscodeLastUnlocked    = @"kPasscodeLastUnlocked";

@implementation PasscodeLock

#pragma mark PasscodeLock

- (BOOL)isImmediate {
    return self.lastUnlocked == nil;
}

- (void)setImmediate:(BOOL)on {
    if (on) {
        [self resetLastUnlocked];
    } else {
        self.lastUnlocked = nil;        
    }
}

- …
Run Code Online (Sandbox Code Playgroud)

boolean objective-c accessor custom-attributes

3
推荐指数
3
解决办法
7951
查看次数

.Net:如何使用TypeDescriptor.GetProperties获取自定义属性?

我创建了自己的属性来装饰我的对象.

 [AttributeUsage(AttributeTargets.All)]
    public class MyCustomAttribute : System.Attribute { }
Run Code Online (Sandbox Code Playgroud)

当我尝试使用传入我的自定义属性的TypeDescriptor.GetProperties时,即使使用该属性修饰了类型,它也不会返回任何内容.

  var props = TypeDescriptor.GetProperties(
              type, 
              new[] { new Attributes.FlatLoopValueInjection()});
Run Code Online (Sandbox Code Playgroud)

如何让TypeDescriptor.GetProperties识别我的自定义类型?

.net custom-attributes typedescriptor

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

有没有办法将属性应用于首先执行的方法?

如果不使用像PostSharp这样的库,有没有办法设置一个我可以拥有逻辑的自定义属性,当附加到方法时,会执行PRIOR进入该方法吗?

c# attributes webforms custom-attributes asp.net-4.0

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

添加自定义属性

在我正在创建的一个相当复杂的HTML5 webapp中,我发现向一些文档元素添加一些自定义属性很方便.使用jQuery,我发现我可以毫无问题地检索这些属性 - 到目前为止,Chrome,Safari和Firefox,以及我希望,也可以在Android/iPhone移动浏览器上.

问题 - 是这样的用法,注入自定义属性,确定或者我会破坏某些内容.为了理解上下文,我使用jQuery Mobile,使用jQuery和一些jQuery插件.

在相关的说明中,我假设可以使用jQuery检索具有指定属性的所有元素?

html5 attributes custom-attributes custom-data-attribute

3
推荐指数
2
解决办法
3495
查看次数

GetCustomAttribute返回null

有人可以向我解释为什么要Value.GetType().GetCustomAttribute回报null吗?我查看了十个不同的教程,了解如何获取枚举类型成员的属性.无论GetCustomAttribute*我使用哪种方法,我都没有返回自定义属性.

using System;
using System.ComponentModel;
using System.Reflection;

public enum Foo
{
    [Bar(Name = "Bar")]
    Baz,
}

[AttributeUsage(AttributeTargets.Field)]
public class BarAttribute : Attribute
{
    public string Name;
}

public static class FooExtensions
{
    public static string Name(this Foo Value)
    {
        return Value.GetType().GetCustomAttribute<BarAttribute>(true).Name;
    }
}
Run Code Online (Sandbox Code Playgroud)

c# enums custom-attributes

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

ContextBoundObject在等待之后引发远程处理错误

我有一些日志代码,使用ContextBoundObject和ContextAttribute编写拦截方法调用.该代码基于代码项目示例.

这一切都运行良好,直到我们开始使用此库与利用异步和等待的代码.现在我们在运行代码时遇到了远程错误.这是一个重现问题的简单示例:

public class OhMyAttribute : ContextAttribute
{
    public OhMyAttribute() : base("OhMy")
    {
    }
}

[OhMy]
public class Class1 : ContextBoundObject
{
    private string one = "1";
    public async Task Method1()
    {
        Console.WriteLine(one);
        await Task.Delay(50);
        Console.WriteLine(one);
    }
}
Run Code Online (Sandbox Code Playgroud)

当我们调用时,Method1我们RemotingException在第二个上面得到以下内容Console.WriteLine:

Remoting cannot find field 'one' on type 'WindowsFormsApplication1.Class1'.
Run Code Online (Sandbox Code Playgroud)

有没有办法使用内置的C#方法来解决这个问题,还是我们必须看看像PostSharp这样的替代解决方案?

c# custom-attributes async-await

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

自定义属性 - 为枚举获取"不是有效的命名属性参数"

当我尝试使用如下MultiselectComperer值时,为什么会收到此消息:

[Display(ResourceType = typeof(OrdersManagementStrings), Name = "PrintSettings")]
[FilterAttribute(IsMultiselect = true, MultiselectComperer=FilterAttribute.eMultiselectComperer.Or)]
public ePrintSettings PrintSettings { get; set; }
Run Code Online (Sandbox Code Playgroud)

这是自定义属性的代码...所有emuns都是公共的...但我收到此消息:

'MultiselectComperer'不是有效的命名属性参数,因为它不是有效的属性参数类型....

[AttributeUsage(AttributeTargets.All, Inherited = false, AllowMultiple = true)]
public sealed class FilterAttribute : Attribute
{
    public enum eMultiselectComperer
    {
        Or,
        And
    }

    public bool IsMultiselect { get; set; }

    public eMultiselectComperer? MultiselectComperer { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

c# asp.net-mvc attributes custom-attributes

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

ASP.NET核心筛选器-忽略定义的方法

我们将日志信息实现到我们的数据库中。我将为其使用过滤器(IActionFilter)功能。我写了下面的课:

public class ActionFilter: Attribute, IActionFilter
{
    DateTime start;
    public void OnActionExecuting(ActionExecutingContext context)
    {
        start = DateTime.Now;
    }

    public void OnActionExecuted(ActionExecutedContext context)
    {
        DateTime end = DateTime.Now;
        double processTime = end.Subtract(start).TotalMilliseconds;
        ... some log actions
    }
}
Run Code Online (Sandbox Code Playgroud)

然后,我将以下代码添加到Startup.cs中:

services.AddMvc(options => {
            options.Filters.Add(typeof(ActionFilter));

        });
Run Code Online (Sandbox Code Playgroud)

工作正常。我在每个方法的ActionFilter中都有一个断点。

但是我想忽略大部分方法的日志记录。据我了解,我可以使用自己的属性来实现。我以前没有使用自己的属性。好的,我写了以下属性:

public class IgnoreAttribute : Attribute
{
    public IgnoreAttribute()
    { }
}
Run Code Online (Sandbox Code Playgroud)

我将属性添加到方法:

[Ignore]
    [HttpGet]
    [Route("api/AppovedTransactionAmountByDays/{daysCount}")]
    public JsonResult GetAppovedTransactionAmountByDays(int daysCount)
    {
        var result = daysCount;

        return new JsonResult(result);
    }
Run Code Online (Sandbox Code Playgroud)

当然,简单的操作是行不通的。

我如何更改我的属性或ActionFilter以忽略方法?

提前致谢。

c# asp.net custom-attributes asp.net-core-mvc asp.net-core

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