标签: custom-attributes

.NET,C#:如何添加充当ISerializable接口的自定义序列化属性

我正在做一些db linq对象的序列化,它包含EntitySet和EntityRef类.

我找到了一种非常简单的方法来处理这些类的序列化,只需使用ISerializable来正确处理这种类型的成员(将它们转换为序列化列表,并在反序列化时撤消它).

但是,如果我能这样做,那将是非常好的:

[Serializable]
[SerializeLinqEntities]
partial class Person
{ ... }
Run Code Online (Sandbox Code Playgroud)

代替:

partial class Person : ISerializable
{
  public virtual void GetObjectData( SerializationInfo si, StreamingContext ctxt )
  {
    EntitySerializer.Serialize(this, typeof(Person), si, ctxt);
  }

  protected Person( SerializationInfo si, StreamingContext ctxt )
  {
    EntitySerializer.Deerialize(this, typeof(Person), si, ctxt);
  }
}
Run Code Online (Sandbox Code Playgroud)

有没有办法做到这一点?我查看了序列化类,似乎无法找到任何方法来设置自定义序列化过滤器例程(我可以在其中查找我的自定义属性).

谢谢!

.net c# serialization custom-attributes

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

根据客户组显示自定义产品属性(Magento)

我对Magento的一家商店的某些产品有批发属性.我想设置它,以便这些特定属性仅出现在产品页面上,如果客户已登录且它们位于批发客户组中.

这可能吗?

store custom-attributes magento

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

下一个视图id作为android中的自定义属性

对于我的自定义视图,我还定义了一个自定义属性来保持视图的id.它的格式是"参考".

在布局xml中,它定义如下,与android:layout_belowattr 非常相似

<mycustomview id="@+id/cv_1" xyz:nextviewId="@id/cv_2"... />
<mycustomview id="@+id/cv_2" xyz:nextviewId="@id/cv_3"... />
...
<LinearLayout ...>
    <mycustomview id="@+id/cv_3" xyz:nextviewId="@id/cv_4"... />
</LinearLayout>
...
Run Code Online (Sandbox Code Playgroud)

它给了我错误我认为这是因为它尚未宣布.

有关访问下一个对象的任何建议类似于这种方法!

我正在考虑使用tag attr为下一个对象找到具有findByTag函数的下一个对象.这是一个很好的方法吗?

非常感谢.

android custom-attributes custom-view

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

为什么不从其他包调用Perl属性处理程序?

我对Attribute :: Handlers有一个奇怪的问题,看起来像某种bug:

package MyPackage;

use Attribute::Handlers;

sub UNIVERSAL::foo :ATTR(CODE) {
  ...
}
Run Code Online (Sandbox Code Playgroud)

当在MyPackage中使用时,或者从使用MyPackage的脚本的主包中使用时,只要编译器遇到表单的函数,就会调用foo处理程序

sub bar:foo {
 ...
}
Run Code Online (Sandbox Code Playgroud)

但是,我在另一个使用MyPackage的.pm文件中有另一个包.编译器接受":foo"属性,但调用处理程序.

我尝试在MyPackage中编写一个导入函数,将foo处理程序导出到调用者的命名空间,但这似乎没有帮助.

任何人都可以理解这个吗?在过去的几天里,我一直在努力解决这个问题.

perl attributes custom-attributes

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

可以将扩展方法添加到类属性以获取与属性关联的属性的值吗?

我有几个类,分配了属性.我最感兴趣的是FieldLength.MaxLength值.

/// <summary>
/// Users
/// </summary>
[Table(Schema = "dbo", Name = "users"), Serializable]
public partial class Users
{

    /// <summary>
    /// Last name
    /// </summary>
    [Column(Name = "last_name", SqlDbType = SqlDbType.VarChar)]
    private string _LastName;
    [FieldLength(MaxLength=25), FieldNullable(IsNullable=false)]
    public string LastName
    {
        set { _LastName = value; }
        get { return _LastName; }
    }

}
Run Code Online (Sandbox Code Playgroud)

我需要知道是否可以为我的类中的属性编写某种扩展方法来返回FieldLength属性的MaxLength值?

例如.我希望能够写出如下内容......

Users user = new Users();
int lastNameMaxLength = user.LastName.MaxLength();
Run Code Online (Sandbox Code Playgroud)

c# extension-methods custom-attributes propertyinfo

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

无法在MVC3 HTML Helper中获取自定义属性值

我用一个需要来自模型属性的属性值的方法扩展了HTML帮助器.所以我已经定义了一个自定义属性.

    public class ChangeLogFieldAttribute : Attribute {
        public string FieldName { get; set; }
    }
Run Code Online (Sandbox Code Playgroud)

在我的模型中使用它就像这样.

    [Display(Name = "Style")]
    [ChangeLogField(FieldName = "styleid")]
    public string Style { get; set; }
Run Code Online (Sandbox Code Playgroud)

在我的帮助方法中,如果属性用于属性,我有以下代码来获取属性的FieldName值.

        var itemName = ((MemberExpression)ex.Body).Member.Name;

        var containerType = html.ViewData.ModelMetadata.ContainerType;
        var attribute = ((ChangeLogFieldAttribute[])containerType.GetProperty(html.ViewData.ModelMetadata.PropertyName).GetCustomAttributes(typeof(ChangeLogFieldAttribute), false)).FirstOrDefault();
        if (attribute != null) {
            itemName = attribute.FieldName;
        }
Run Code Online (Sandbox Code Playgroud)

但是,当我到达此代码时,我得到一个异常,因为containerType为null.

我不确定我是否正在做任何正确的事,但是我从大约4个不同的来源中取得了这一点.如果您可以建议解决我的问题或替代方案,我将不胜感激.

谢谢.

更新解决方案

我使用了Darin Dimitrov的解决方案,虽然我不得不调整它.这是我添加的内容.我必须检查属性metatdata的存在,一切都很好.

        var fieldName = ((MemberExpression)ex.Body).Member.Name;

        var metadata = ModelMetadata.FromLambdaExpression(ex, html.ViewData);
        if (metadata.AdditionalValues.ContainsKey("fieldName")) { 
            fieldName = (string)metadata.AdditionalValues["fieldName"];
        }
Run Code Online (Sandbox Code Playgroud)

custom-attributes data-annotations asp.net-mvc-3

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

C#属性用法:仅允许具有特定数据类型的属性上的属性

我已经为属性创建了一些属性.现在我想将这些属性限制为具有特定数据类型的属性?这个想法是,如果将其分配给不同的类型,则会抛出编译器错误.这可能吗?

如果没有,那么我想我将不得不在运行时检查它.

c# custom-attributes

5
推荐指数
2
解决办法
3475
查看次数

将方法的参数转换为自定义动作过滤器MVC3 asp

我一直在研究一个应用程序,每次你注册,修改,取消,删除等......某些事情,通知必须发送给用户(如果配置)所以我打算通过保存来做到这一点数据库上进程的唯一标识符,然后检查通知是否配置了id,proccess和唯一标识符,然后发送通知.

要做到这一点,我记住这一点......这在控制器上

   [NotificationFilter(id=10,proccess="Excecution") ]
   public Register(Entity entity,Guid uid){

   }
Run Code Online (Sandbox Code Playgroud)

这个在ActionFilter类上

public class NotificationFilter : ActionFilterAttribute
{
    public int id{ get; set; }
    public string proccess{ get; set; }

    public override void OnActionExecuted(ActionExecutedContext filterContext)
    {

         dosomething(id,name,uid)
    }
}
Run Code Online (Sandbox Code Playgroud)

但我想知道是否有办法将我的Register操作的uid参数放入OnActionExecuted方法.

c# custom-attributes action-filter asp.net-mvc-3

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

设置ASP.NET Button属性客户端和读取属性值服务器端

如何Button在使用javascript更改属性值后检索自定义属性?

例:

Asp文件

<asp:Button ID="Button1" runat="server" Text="Button1" />
<asp:Button ID="Button2" runat="server" Text="Button2" OnClick="Button2_Click" />

<script type="text/javascript">
var btn1 = '#<% Button1.ClientID %>';
var btn2 = '#<% Button2.ClientID %>';

$(btn1).click(function(e) {
    e.preventDefault();
    $(btn2).attr("actIndex", "2");
});

</script>
Run Code Online (Sandbox Code Playgroud)

CodeBehind文件

protected void Page_Load(object sender, EventArgs e)
{
    if(!IsPostBack)
        Button2.Attributes.Add("actIndex","1");
}

protected void Button2_Click(object sender, EventArgs e)
{
     Button btn = (Button)sender;

     // this should be 2 if button1 has been clicked
     string actIndex = btn.Attributes["actIndex"]; 
}
Run Code Online (Sandbox Code Playgroud)

如果我单击Button1然后单击Button2actIndex …

javascript c# asp.net postback custom-attributes

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

C#MVC - 根据模型动态获取属性的名称

在我的MVC项目中,我有不同的自定义验证属性.其中之一是检查属性的值与另一个属性的值.

正如许多文章中所述,我添加了类似的内容

result.ValidationParameters.Add("otherproperty", _otherPropertyHtml);
result.ValidationParameters.Add("comparetype", _compareType);
result.ValidationParameters.Add("equalitytype", _equalityType);
Run Code Online (Sandbox Code Playgroud)

返回的ModelClientValidationRule对象.

我现在的问题是,如果我要检查的属性被封装在另一个对象中,验证将无效.

如果我创造类似的东西

@Html.TextBoxFor(m => m.ValueOne)
@Html.TextBoxFor(m => m.ValueTwo)
Run Code Online (Sandbox Code Playgroud)

验证将在呈现时正常工作

data-val-otherproperty="ValueTwo"
Run Code Online (Sandbox Code Playgroud)

我的问题是以下几点

@Html.TextBoxFor(m => m.IntermediateObject.ValueOne)
@Html.TextBoxFor(m => m.IntermediateObject.ValueTwo)
Run Code Online (Sandbox Code Playgroud)

这将呈现两个带有名称IntermediateObject_ValueOne和的文本框IntermediateObject.ValueTwo.但仍然是第一个文本框的data-val-otherproperty ="ValueOne".

如何才能实现,这data-val-otherproperty一直是其他财产的正确名称?

我的想法类似于HtmlHelper <>.NameFor(m => ...)或使用反射的东西?

更新1 - 根据评论的要求添加代码

[AttributeUsage(AttributeTargets.Property | AttributeTargets.Class, AllowMultiple = false)]
public class CustomCompareToOther : ValidationAttribute, IClientValidatable
{
    // private backing-field
    private readonly string _otherPropertyName;

    // constructor
    public OemCompareToOther(string otherPropertyName)
    {
        _otherPropertyName = otherPropertyName;
    }

    // implementation of IClientValidatable
    public IEnumerable<ModelClientValidationRule> …
Run Code Online (Sandbox Code Playgroud)

c# reflection validation asp.net-mvc custom-attributes

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