custom-attributes

Advantage of using CustomAttributes vs GetCustomAttributes()

北城余情 提交于 2020-01-13 09:34:11
问题 I noticed today that some new properties had appeared in my intellisense on the System.Type object for my .NET 4.5 projects. Among these was one called CustomAttributes . I was intrigued by this since I previously had understood that GetCustomAttributes was one of the most expensive reflection calls ( DynamicInvoke and the like aside, of course). As I understand it, every call to GetCustomAttributes results in calling the constructors for the attributes (and thus a memory allocation). I've

Passing a type as parameter to an attribute

一笑奈何 提交于 2020-01-13 08:30:30
问题 I wrote a somewhat generic deserialization mechanism that allows me to construct objects from a binary file format used by a C++ application. To keep things clean and easy to change, I made a Field class that extends Attribute , is constructed with Field(int offset, string type, int length, int padding) and is applied to the class attributes I wish to deserialize. This is how it looks like : [Field(0x04, "int")] public int ID = 0; [Field(0x08, "string", 0x48)] public string Name = "0"; [Field

How can I implement permission-based authorization in ASP.NET?

旧城冷巷雨未停 提交于 2020-01-11 19:55:06
问题 I'm working an a ASP.NET application (not using MVC) and need a User-Role-Permission based authorization scheeme, where pages and/or methods can demand the specific permission they require (instead of which role the user has). Is there a way to extend Forms Authentication (or building something) to solve this? If possible I would like to be able to use attributes: [RequirePermission("UserEdit")] public partial class EditUser : System.Web.UI.Page { } Perhaps even for methods: public class

How can I implement permission-based authorization in ASP.NET?

孤人 提交于 2020-01-11 19:53:19
问题 I'm working an a ASP.NET application (not using MVC) and need a User-Role-Permission based authorization scheeme, where pages and/or methods can demand the specific permission they require (instead of which role the user has). Is there a way to extend Forms Authentication (or building something) to solve this? If possible I would like to be able to use attributes: [RequirePermission("UserEdit")] public partial class EditUser : System.Web.UI.Page { } Perhaps even for methods: public class

.NET, C#: How to add a custom serialization attribute that acts as ISerializable interface

狂风中的少年 提交于 2020-01-10 19:56:07
问题 I am doing some serialization of db linq objects, which contain EntitySet and EntityRef classes. I found a pretty easy way to deal with serialization of these classes, by simply using ISerializable to properly handle members of this type (converting them to lists for serialization, and undoing it on deserialization). However, it would be really nice if I could do: [Serializable] [SerializeLinqEntities] partial class Person { ... } Instead of: partial class Person : ISerializable { public

Why are classLoader magic values in defineClass() throwing an exception?

梦想的初衷 提交于 2020-01-07 09:28:05
问题 I want to write a classLoader that can help me implement customized classes and ultimately a whole component at run time. Right now I'm in process of loading the class. I'm trying to load this role.java file. However when I get to this part of the code: myClass = super.defineClass(className, classData, 0, classData.length); I get this exception: Exception in thread "main" java.lang.ClassFormatError: Incompatible magic value 1885430635 in class file C:\Users\ARIFAH\Downloads\Compressed\eUML2

reflection: private property how to extract custom attribute

浪尽此生 提交于 2020-01-07 03:16:05
问题 Hi it's possible to retrieve custom attribute in private property public class TestAttr { [SaveInState] protected string testPrivate { get { return "test private"; } } [SaveInState] public string testPublic { get{ return "test public"; }} public IDictionary<string, object> dumpVars() { IDictionary<string, object> dict = new Dictionary<string, object>(); Type ownerClassType = this.GetType(); foreach (var mi in ownerClassType.GetProperties(BindingFlags.NonPublic)) { var varAttrib = Attribute

Controller Attribute Check like using [FromBody] twice

可紊 提交于 2020-01-06 09:06:08
问题 In ASP.NET MVC (regardless of version, if it's important, assume I am using the latest Core 2.1) we control the behavior and parameter binding of the app by annotating the controller with attributes, for example [HttpPost] for a method that is supposed to be called as POST and [FromQuery] for a method parameter that is supposed to come from the query string. Now as I have found out the hard way in the last months, there are countless ways to mix this up. If you have two parameters declared as

How to add Custom Attributes to a DynamicMethod-generated method?

别等时光非礼了梦想. 提交于 2020-01-05 03:27:08
问题 I was playing around with DynamicMethod and Expression Trees' Compilation (which uses DynamicMethod internally). I then wondered if there is a way to add a custom attribute to the generated method. I googled about it, but I couldn't find a way. I know that it's possible to do using CodeDom, but I want to use DynamicMethod . Someone mentioned Type Descriptor, but I'm not sure if it helps. Does anyone knows a way to define custom attributes to methods generated using DynamicMethod ? 回答1: No

Custom vs. non-custom attributes?

天大地大妈咪最大 提交于 2020-01-03 08:14:10
问题 Something which implements the ICustomAttributeProvider interface will allow you to get custom attributes that have been applied to it via the GetCustomAttributes method. As I understand it, a custom attribute is basically a special class (ending in "Attribute" and extending the Attribute class) that is created to be applied to something like a method or class using the appropriate syntax ( [FooAttribute] just before the method/class/etc. in C#, for example). But if that is a custom attribute