相关疑难解决方法(0)

接口成员的属性不起作用

在我的应用程序中,有几个模型需要Password属性(例如RegistrationChangePassword模型).该Password物业具有DataType和的属性Required.因此,为了确保可重用性的一致性,我创建了:

interface IPasswordContainer{
    [Required(ErrorMessage = "Please specify your password")]
    [DataType(DataType.Password)]
    string Password { get; set; }
} 
Run Code Online (Sandbox Code Playgroud)

class RegistrationModel : IPasswordContainer {
    public string Password { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

不幸的是,属性不起作用.

然后我尝试将界面更改为类:

public class PasswordContainer {
    [Required(ErrorMessage = "Please specify your password")]
    [DataType(DataType.Password)]
    public virtual string Password { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

public class RegistrationModel : PasswordContainer {
    public override string Password { get; set; } …
Run Code Online (Sandbox Code Playgroud)

c# attributes annotations interface

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

从接口方法和类方法中获取属性

什么是在方法重载时从类方法和接口方法获取属性值的最佳方法?

例如,我想知道在下面的示例中,带有一个参数的Get方法具有两个属性,值为5和"any",而另一个方法具有值为7和"private"的属性.

public class ScopeAttribute : System.Attribute
{
    public string Allowed { get; set; }    
}

public class SizeAttribute : System.Attribute
{
    public int Max { get; set; }
}

public interface Interface1
{
    [SizeAttribute( Max = 5 )]
    string Get( string name );

    [SizeAttribute( Max = 7 )]
    string Get( string name, string area );

}

public class Class1 : Interface1
{
    [ScopeAttribute( Allowed = "any" )]
    public string Get( string name )
    {
        return string.Empty;
    }

    [ScopeAttribute( Allowed …
Run Code Online (Sandbox Code Playgroud)

c# reflection custom-attributes

10
推荐指数
2
解决办法
6770
查看次数

MVC通用ViewModel

总之,我希望能够将通用的ViewModel传递给我的视图

以下是我想要实现的目标的一些简化代码

public interface IPerson
{
    string FirstName {get;}
    string LastName {get;}
}

public class FakePerson : IPerson
{
    public FakePerson()
    {
        FirstName = "Foo";
        LastName = "Bar";
    }

    public string FirstName {get; private set;} 
    public string LastName {get; private set;} 
}

public class HomeViewModel<T> 
    where T : IPerson, new()
{
    public string SomeOtherProperty{ get; set;}
    public T Person { get; private set; }

    public HomeViewModel()
    {
        Person = new T();
    }
}

public class HomeController : Controller {
    public …
Run Code Online (Sandbox Code Playgroud)

asp.net generics asp.net-mvc viewmodel

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

C#界面问题

如果我想要一堆类来实现一个方法,我可以让它们实现一个接口.但是,如果我希望方法始终使用两个自定义属性进行修饰,是否有语法?换句话说,我希望每个实现方法Run()的类都附加一个descriptionAttribute和一个versionAttribute.

更新:有没有办法让实现Run()的类生成编译错误,如果他们没有附加这两个属性?

c#

7
推荐指数
2
解决办法
322
查看次数

检查通用接口成员是否为"纯粹"(具有纯属性)

我有一个接口,其方法使用以下Pure属性进行注释System.Diagnostics.Contracts:

public interface IFoo<T> {
    [Pure]
    T First { get; }

    [Pure]
    T Last { get; }

    [Pure]
    T Choose();

    void Add(T item);

    T Remove();
}
Run Code Online (Sandbox Code Playgroud)

我希望遍历接口的成员并检查成员是否是纯粹的.目前我无法从会员信息中获取任何属性:

var type = typeof(IFoo<>);
var memberInfos = type.GetMembers();
var memberInfo = memberInfos.First(); // <-- Just select one of them
var attributes = memberInfo.GetCustomAttributesData(); // <-- Empty
Run Code Online (Sandbox Code Playgroud)

我错过了什么?

请注意,我没有类或实例.只有界面.

c# generics reflection interface custom-attributes

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

界面上的属性有什么用?

使用Resharper,我提取了现有类的接口.这个类在一些成员上设置了一些属性,Resharper也将这些属性放在接口成员上.

我可以从界面中删除这些属性吗?实现接口时,接口上的属性不会被继承吗?

.net attributes interface

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

在 C# 中,接口中定义的属性是否适用于实现类?

我从很多类似的属性在接口中定义的地方读DO NOT适用于实现类,并简单地被忽略:

然而,似乎这样的属性DO适用于实现类:

using Newtonsoft.Json;

interface TestInterface
{
    [JsonProperty(PropertyName = "foo")]
    string id { get; set; }
}

class Test : TestInterface
{
    public string id { get; set; }
}

class Program
{
    static void Main(string[] args)
    {
        Console.WriteLine(JsonConvert.SerializeObject(new Test()));
    }
}

Console Output: {"foo": null}
Run Code Online (Sandbox Code Playgroud)

在上面的例子中,JsonProperty 属性显然被应用在实现类中(将“id”字段名称更改为“foo”以进行序列化)。

界面中 C# 属性的行为是否发生了变化,还是我遗漏了什么?

c# attributes interface

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