所以有点忏悔,我从来没有写过属性类.我知道他们的目的是用标志或额外的功能来装饰类.
有人可以给我一个简单的例子,不仅可以创建属性并将其应用于类,而且可以使用另一个代码块中的属性.我见过的唯一使用任何形式属性的代码示例都是通过反射来实现的,尽管我一直希望有一种方法可以在没有反射的情况下使用它们.
我在选择正确的异常类型时遇到一些麻烦时遇到了一些麻烦(我更喜欢现有的 .NET 异常之一)。
在这种情况下你有什么建议?提前致谢。
编辑:
这是他的背景:
[<ExpectedAttribute()>]
let foo args ... = ...
Run Code Online (Sandbox Code Playgroud)
该函数foo(用户定义的)被传递到运行时引擎。如果自定义属性不存在,运行时必须抛出异常。
我想在运行时在 C# 中创建自定义对象,这些对象将具有从 xml 文件导入的属性。xml 文件如下所示:
<field name="FirstName" value="Joe" type="string" />
<field name="DateAdded" value="20090101" type="date" />
Run Code Online (Sandbox Code Playgroud)
我想在 c# 中创建对象,这些对象具有 FirstName 和 DateAdded 等属性,并且具有正确的属性类型。我怎样才能做到这一点?我尝试使用带有 if 语句的函数来根据“type”属性确定类型,但我也想即时评估类型。
谢谢。
编辑:看起来不可能像解决方案#2一样只有一个属性,所以我最终使用的解决方案#3不像解决方案#1那样容易出错.
我有一个属性如下:
// Solution #1 (working)
[Module(Name="Module1", Guid="xxxxx",
NeededFiles_Name = new string[]
{
"module1Conf.xml",
"module1.sql",
"resourcesWindows",
"resourcesUnix",
},
NeededFiles_OS = new OSType[]
{
All,
All,
Windows,
Unix,
}
NeededFiles_Component = new ComponentType[]
{
Server,
Server,
All,
All
}
)]
Run Code Online (Sandbox Code Playgroud)
问题是填充NeededFile_Xxx属性并不容易.只有一个属性会更容易
// Solution #2 (NOT working)
[Module(Name="Module1", Guid="xxxxx",
NeededFile = new FileInfo[]
{
new FileInfo("module1Conf.xml", All, Server),
...
}
)]
Run Code Online (Sandbox Code Playgroud)
但是编译器不允许它(NeededFile不是有效的命名属性参数,因为它不是有效的属性参数类型.
我也可以将我的属性划分为几个并且可以
// Solution #3 (working)
[Module(Name="Module1", Guid="xxxxx"]
[NeededFile("module1Conf.xml", All, Server)]
[NeededFile("module1.sql", All, Server)]
[NeededFile(...)]
Run Code Online (Sandbox Code Playgroud)
但我宁愿把一切都放在一边.
有可能吗?有没有更好的方法呢?
[AttributeUsage(AttributeTargets.Method,AllowMultiple=true)]
public class MethodId : Attribute
{
private int mId;
public MethodId(int mId)
{
this.mId = mId;
}
public int methodId
{
get { return this.mId; }
set { this.mId = value; }
}
}
Run Code Online (Sandbox Code Playgroud)
public class Methods
{
[MethodId(1)]
public void square()
{ }
[MethodId(2)]
public void Notify()
{ }
}
Run Code Online (Sandbox Code Playgroud)
如何在MethodId的帮助下访问main()或任何其他类中的square()?
如何为我的自托管 WCF 服务添加可选的 gzip 压缩?我在这个场景中使用WebHttpBinding. 我想检查Accept标头是否包含字符串gzip并压缩内容。
我想使用自定义属性。到目前为止,我目前使用自定义属性允许我在 XML 和 JSON 输出之间切换,但我现在不知道如何压缩输出。
在我的编码器开关属性中,我实现了IDispatchMessageFormatter按需更改XmlObjectSerializer. 但我不明白如何生成输出来修改它。
如果有人能给我指出一个可能的解决方案,那就太好了。
问题不是那么简单:).
基本上,我需要知道的是将从一系列方法的任何方法返回的数组中包含的内容Attribute.GetCustomAttributes().
当然,我定义的所有属性都将存在.但是,不仅仅是他们.例如,所有实体框架属性也EdmScalarPropertyAttribute都存在.什么属性不会被视为自定义?
我有一个简单的POCO类,例如
class C {
[MyAtrib]
public int i {get; set;}
[MyAtrib]
public int i2;
}
Run Code Online (Sandbox Code Playgroud)
我打电话的时候:
GetType().GetFields(
BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance);
Run Code Online (Sandbox Code Playgroud)
在那个类(实例)上我无法得到FieldInfo那些自动生成getter/setter(即int i上面)的成员.
实际上,我正在尝试读取那些自定义属性(MyAtrib),而不能为那些具有的属性执行此操作{get; set;}.
这是为什么?我希望得到这两个i和它的(私人)支持领域,因为i是公开的.
我能到i的MyAtrib某种方式通过反射?
在以下代码中,访问 a 的自定义属性会SomeClass导致哈希函数SomeAttribute变得不稳定。这是怎么回事?
static void Main(string[] args)
{
typeof(SomeClass).GetCustomAttributes(false);//without this line, GetHashCode behaves as expected
SomeAttribute tt = new SomeAttribute();
Console.WriteLine(tt.GetHashCode());//Prints 1234567
Console.WriteLine(tt.GetHashCode());//Prints 0
Console.WriteLine(tt.GetHashCode());//Prints 0
}
[SomeAttribute(field2 = 1)]
class SomeClass
{
}
class SomeAttribute : System.Attribute
{
uint field1=1234567;
public uint field2;
}
Run Code Online (Sandbox Code Playgroud)
现在已将此作为错误报告给 MS。 https://connect.microsoft.com/VisualStudio/feedback/details/3130763/attibute-gethashcode-unstable-if-reflection-has-been-used
此问题现已在 dotnetcore 中得到解决:https : //github.com/dotnet/coreclr/pull/13892
我有一个 .NET Core 1.1 应用程序,在 HomeController 中的操作上设置了自定义属性。鉴于我需要属性逻辑内的配置文件 (appsettings.json) 中的值,是否可以在属性级别访问配置?
appsettings.json
{
"Api": {
"Url": "http://localhost/api"
}
}
Run Code Online (Sandbox Code Playgroud)
HandleSomethingAttribute.cs
public class HandleSomethingAttribute : Attribute, IActionFilter
{
public void OnActionExecuting(ActionExecutingContext context)
{
// read the api url somehow from appsettings.json
}
public void OnActionExecuted(ActionExecutedContext context)
{
}
}
Run Code Online (Sandbox Code Playgroud)
家庭控制器.cs
public class HomeController: Controller
{
[HandleSomething]
public IActionResult Index()
{
return View();
}
}
Run Code Online (Sandbox Code Playgroud) custom-attributes appsettings asp.net-core-mvc asp.net-core-1.1
c# ×6
.net ×2
attributes ×2
reflection ×2
appsettings ×1
c#-4.0 ×1
exception ×1
gzip ×1
hash ×1
invoke ×1
object ×1
properties ×1
rest ×1
wcf ×1