标签: custom-attributes

在 C# 中使用自定义属性是内存/性能瓶颈吗?

我想用自定义属性装饰我的方法和 GUI 控件。我想知道属性如何消耗内存或影响应用程序性能。什么是属性的生命周期。意味着当一个类的对象在方法、属性和自身中具有自定义属性时。被实例化,然后被释放。如果所有自定义属性实例也随着对象的解构而处理,或者它们仍然保留在内存中?

c# performance custom-attributes

2
推荐指数
1
解决办法
1349
查看次数

如何在Enum上使用多个描述属性

我们如何在Enum上定义和使用多个描述属性?

public enum EnumWithDescription
{
    [CustomDescritption("job-view")]
    [Description("analyics-job-view")]
    JobView
}

class CustomDescritption: DescriptionAttribute
{
    private string extraInfo;
    public string ExtraInfo { get { return extraInfo; } set { extraInfo = value; } }

    public MyDescritptionAttribute(string description)
    {
        this.DescriptionValue = description;
        this.extraInfo = "";
    }
}
Run Code Online (Sandbox Code Playgroud)

c# enums custom-attributes

2
推荐指数
1
解决办法
3107
查看次数

如何使用zend from在zend框架2中添加自定义属性

我想在zend框架项目中使用angularJS,在这个项目中,表单是使用zend形式生成的.如何在表单元素中添加角度指令,如"ng-model",但每当我尝试在zend-form元素中添加此自定义属性(输入,选择等)时,我都没有得到这个属性--- -

这是我的主要形式

class LeadForm扩展Form {

public function __construct() {

    parent::__construct('lead_form');

    $this->setAttributes(array(
        'action' => '',
        'method' => 'post',
        'name' => 'lead_form',
        'id' => 'lead_form',
        'class' => 'smart-form',
        'role' => 'form',
        'novalidate' => 'novalidate'
    ));

    $this->add(array(
        'name' => 'first_name',
        'type' => 'text',
        'options' => array(
            'label' => 'First Name',
        ),
        'attributes' => array(
            'class' => 'form-control validate[required,custom[onlyLetterSp]]',                
            'placeholder' => 'First name',                
            **'ng-model' => "first_name", // this attribute is not generating in view**
        ),            
    ));
}
Run Code Online (Sandbox Code Playgroud)

}

这是我的控制器,它调用此表单并发送到视图进行显示

$createLeadForm = new \Admin\Form\LeadForm(); …
Run Code Online (Sandbox Code Playgroud)

php custom-attributes zend-form zend-framework2 angularjs

2
推荐指数
1
解决办法
3764
查看次数

在OnActionExecuting中获取预期的Action参数类型

问题: 是否可以知道被调用动作所期望的参数类型?例如,我有一些action:

[TestCustomAttr]
public ActionResult TestAction(int a, string b)
{
    ...
Run Code Online (Sandbox Code Playgroud)

TestCustomAttr定义为:

public class TestCustomAttr : System.Web.Mvc.ActionFilterAttribute
{
    public override void OnActionExecuting(ActionExecutingContext filterContext)
    {
        ...
Run Code Online (Sandbox Code Playgroud)

因此,当调用TestAction此处内部时OnActionExecuting,我想知道该TestAction方法所期望的类型.(例如:在这种情况下,有2个预期参数.一个是类型int,另一个是类型string.

实际目的: 实际上我需要更改值QueryString.我已经能够获取查询字符串值(通过HttpContext.Current.Request.QueryString),更改它,然后手动将其添加ActionParametersfilterContext.ActionParameters[key] = updatedValue;

问题: 目前,我尝试将值解析为int,如果它被成功解析,我认为它是一个int,所以我进行需求更改(例如值+ 1),然后将其添加到操作参数,对应其键.

 qsValue = HttpContext.Current.Request.QueryString[someKey].ToString();

 if(Int32.TryParse(qsValue, out intValue))
 {
     //here i assume, expected parameter is of type `int`
 }
 else
 {
     //here i assume, …
Run Code Online (Sandbox Code Playgroud)

c# custom-attributes query-string onactionexecuting asp.net-mvc-4

2
推荐指数
1
解决办法
2646
查看次数

自动运行方法的 C# 属性

我不确定这是否完全可能。但我想做的是创建一个属性,当我调用 run 方法时,然后运行具有特定 run 属性的所有方法。我意识到这可以通过委托来完成,但我觉得如果可以通过属性来实现,看起来可能会更干净一些。我应该指出,运行顺序并不重要。

基本设计:

//This is the method called that should start off the attribute chain
public void Run(){
  //calling logic in here
}

[AutomatedRun]
private void Method1(){

}

[AutomatedRun]
private void Method2(){

}
Run Code Online (Sandbox Code Playgroud)

custom-attributes c#-4.0

2
推荐指数
1
解决办法
1525
查看次数

TypeBuilder - 添加属性

我有一个使用 TypeBuilder 来构造动态类型的辅助类。它的用法如下:

var tbh = new TypeBuilderHelper("MyType");
tbh.AddProperty<float>("Number", 0.0f);
tbh.AddProperty<string>("String", "defaultStringValue");
tbh.Close();

var i1 = tbh.CreateInstance();
var i2 = tbh.CreateInstance();
Run Code Online (Sandbox Code Playgroud)

我现在想添加对属性属性(现有属性类型,而不是动态生成的类型)的支持,如下所示:

public class TypeBuilderHelper
    {
        public void AddProperty<T>(string name, T defaultValue, params Attribute[] attributes)
        {
            // ...
        }
    }

public class SomeAttribute : Attribute
    {
        public SomeAttribute(float a) { }
        public SomeAttribute(float a, int b) { }
        public SomeAttribute(float a, double b, string c) { }
    }

 var tbh2 = new TypeBuilderHelper("MyType2");
 tbh2.AddProperty<float>("Number", 0.0f, new SomeAttribute(0.0f, 1));
 tbh2.AddProperty<string>("String", "defaultStringValue"); …
Run Code Online (Sandbox Code Playgroud)

c# properties dynamic custom-attributes typebuilder

2
推荐指数
1
解决办法
2172
查看次数

有没有办法让 C# 自定义属性在编译时引发消息?

我的一位同事正在使用ObsoleteAttribute尚未实现的方法,因此我们在编译时收到警告。

然而,由于要实现的方法与过时的方法完全相反,这让我很烦恼。

NotYetImplementedButPleaseBePatientWeVeGotLoadsOfOtherThingsToDoAsWellAttribute我在文档中没有看到 a ,所以我想也许我们可以创建一个。

ObsoleteAttributesealed,所以我们不能继承它。(是的,我在尝试时发现了这一点。很好的尝试,我。)

有没有其他方法可以模仿ObsoleteAttribute,但有一个更合适的名字?

.net c# custom-attributes compile-time

2
推荐指数
1
解决办法
315
查看次数

如何从基类调用GetCustomAttributes?

我需要能够从其基类中的方法检索类的自定义属性.现在我通过基类中的受保护的静态方法执行此操作,具有以下实现(该类可以应用相同属性的多个实例):

//Defined in a 'Base' class
protected static CustomAttribute GetCustomAttribute(int n) 
{
        return new StackFrame(1, false) //get the previous frame in the stack
                                        //and thus the previous method.
            .GetMethod()
            .DeclaringType
            .GetCustomAttributes(typeof(CustomAttribute), false)
            .Select(o => (CustomAttribute)o).ToList()[n];
}
Run Code Online (Sandbox Code Playgroud)

我这样从派生类中调用它:

[CustomAttribute]
[CustomAttribute]
[CustomAttribute]
class Derived: Base
{
    static void Main(string[] args)
    {

        var attribute = GetCustomAttribute(2);

     }

}
Run Code Online (Sandbox Code Playgroud)

理想情况下,我可以从构造函数中调用它并缓存结果.

谢谢.

PS

我意识到GetCustomAttributes不保证在词法顺序方面返回它们.

c# reflection inheritance custom-attributes

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

在不调用getter的情况下读取属性的属性?

C#4.0.我有一个属性缓慢的属性.我想在不调用getter的情况下读取此属性:

[Range(0.0f, 1000.0f)]
public float X
{
    get
    {
        return SlowFunctionX();
    }
}
Run Code Online (Sandbox Code Playgroud)

这就是我现在拥有的:

public static T GetRangeMin<T>(T value)
{
    var attribute = value.GetType()
        .GetField(value.ToString())
        .GetCustomAttributes(typeof(RangeAttribute), false)
        .SingleOrDefault() as RangeAttribute;

    return (T)attribute.Minimum;
}

var min = GetRangeMin<double>(X); // Will call the getter of X :(
Run Code Online (Sandbox Code Playgroud)

问:如何在不调用getter的情况下读取此属性X

c# custom-attributes

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

条件属性如何在幕后工作

我们有这个代码:

public static class MyCLass 
{
    [Conditional("Debugging")]  
    public static void MyMethod()
    {
         Console.WriteLine("Example method");
    }
}
.
.
.
//In debug mode: Executing Main method in debug mode
MyClass.MyMethod()
Run Code Online (Sandbox Code Playgroud)

我想知道的是条件属性如何改变MyMethod的行为,假设在.NET中Conditional属性定义为:

public class Conditional: Attribute
{
   .
   .
   public string Mode { get; set; )
   .
   .
   public Conditional(string Mode)
   {
       .
       .
       this.Mode = Mode;
       if (Mode == "Debugging")
       {
          #ifdef DEBUG
          //HOW THE CONDITIONAL CONSTRUCTOR COULD CHANGE THE BEHAVIOUR OF MyMethod
          #endif
       }
       .
       .
   }
}
Run Code Online (Sandbox Code Playgroud)

如何访问由我的属性(即来自MyAttribute类)修饰的资源(方法,成员,类......)?

.net c# debugging conditional custom-attributes

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