我基于过去4或5年使用C#属性的许多设计和框架.
但最近我看到许多人开始不鼓励他们使用或改变他们的框架以减少他们的需要或使用.
我发现它们是天赐之物,但现在我开始怀疑自己错过了什么.
澄清:使用约定优于配置正成为遵循ORM领域的主要原则.此区域是否可以使用配置文件(XML)映射字段,使用属性或具有直接映射到数据库表中字段的通用命名约定.我没有引用任何引用,但我已经阅读了一些反对添加另一个属性的反对意见.
但我觉得在我刚刚列出的三个选项中,属性仍然是最有意义的.Config文件难以维护,常见的命名约定将您与数据库字段的实现联系起来.属性被准确放置在需要它们的位置,并且实现可以在不断开其使用位置的情况下进行更改.
是否可以通过.NET中的属性分析各个方法?
我目前正在尝试在大量遗留应用程序中找到一些瓶颈,这些应用程序大量使用静态方法.目前,集成框架根本不是一种选择.由于大多数调用都使用静态方法,因此接口和依赖注入不可用.攻击代码以记录诊断也不是一个可行的解决方案.
我知道市场上有一些分析工具,但它们目前不在预算范围内.理想情况下,我将能够创建自己的自定义属性,该属性将记录有关方法输入和方法退出的一些基本信息.我从来没有真正使用自定义属性,所以任何洞察甚至是否可能这将是值得赞赏的.
如果可能的话,我想通过配置文件启用分析.这将通过单元和集成测试支持分析.
我知道有人已经问过为BOOL变量编写getter和setter.但是,如果我定义自定义的getter和setter方法setImmediate和isImmediate分别,我想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) 我创建了自己的属性来装饰我的对象.
[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识别我的自定义类型?
如果不使用像PostSharp这样的库,有没有办法设置一个我可以拥有逻辑的自定义属性,当附加到方法时,会执行PRIOR进入该方法吗?
在我正在创建的一个相当复杂的HTML5 webapp中,我发现向一些文档元素添加一些自定义属性很方便.使用jQuery,我发现我可以毫无问题地检索这些属性 - 到目前为止,Chrome,Safari和Firefox,以及我希望,也可以在Android/iPhone移动浏览器上.
问题 - 是这样的用法,注入自定义属性,确定或者我会破坏某些内容.为了理解上下文,我使用jQuery Mobile,使用jQuery和一些jQuery插件.
在相关的说明中,我假设可以使用jQuery检索具有指定属性的所有元素?
有人可以向我解释为什么要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) 我有一些日志代码,使用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这样的替代解决方案?
当我尝试使用如下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) 我们将日志信息实现到我们的数据库中。我将为其使用过滤器(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# ×6
attributes ×4
.net ×2
accessor ×1
asp.net ×1
asp.net-4.0 ×1
asp.net-core ×1
asp.net-mvc ×1
async-await ×1
boolean ×1
enums ×1
html5 ×1
objective-c ×1
profiling ×1
webforms ×1