ben*_*asd 2 c# implicit-conversion data-annotations explicit-conversion
我们在域模型中使用了自定义LocalizedString类型.我们想用验证属性来装饰属性MaxLength.为此,我们添加了隐式运算符以启用此属性所需的强制转换.
奇怪的是,运算符似乎永远不会被调用,并且在属性IsValid方法中抛出了InvalidCastException .在我们自己的项目中执行此演员表.
在这个系统clr ngen'ed属性中有没有特殊的强制转换行为编译器magix?
// Custom type
public class LocalizedString
{
public string Value
{
get { return string.Empty; }
}
public static implicit operator Array(LocalizedString localizedString)
{
if (localizedString.Value == null)
{
return new char[0];
}
return localizedString.Value.ToCharArray();
}
}
// Type: System.ComponentModel.DataAnnotations.MaxLengthAttribute
// Assembly: System.ComponentModel.DataAnnotations, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35
// Assembly location: C:\Windows\Microsoft.NET\Framework\v4.0.30319\System.ComponentModel.DataAnnotations.dll
public override bool IsValid(object value)
{
this.EnsureLegalLengths();
if (value == null)
{
return true;
}
else
{
string str = value as string;
int num = str == null ? ((Array) value).Length : str.Length;
if (-1 != this.Length)
return num <= this.Length;
else
return true;
}
}
[TestMethod]
public void CanCallIsValidWithLocalizedString()
{
// Arrange
var attribute = new MaxLengthAttribute(20);
var localized = new LocalizedString { Value = "123456789012345678901" };
// Act
var valid = attribute.IsValid(localized);
// Assert
Assert.IsFalse(valid);
}
Run Code Online (Sandbox Code Playgroud)
谢谢你的帮助.
编辑
Das Objekt des Typs "Nexplore.ReSearch.Base.Core.Domain.Model.LocalizedString" kann nicht in Typ "System.Array" umgewandelt werden.
bei System.ComponentModel.DataAnnotations.MaxLengthAttribute.IsValid(Object value)
bei Nexplore.ReSearch.Base.Tests.Unit.Infrastructure.CodeFirst.MaxLengthLocalizedAttributeTests.CanCallIsValidWithLocalizedString() in d:\Nexplore\SourceForge\Nexplore.ReSearch.Base\Source\Nexplore.ReSearch.Base.Tests.Unit\Infrastructure.CodeFirst\MaxLengthLocalizedAttributeTests.cs:Zeile 40.
Run Code Online (Sandbox Code Playgroud)
任何类型的运算符仅在编译时已知对象类型时才适用.它们不是"动态"应用于object.
您可以尝试使用dynamic哪个可以做到这一点.
例子:
using System;
class Foo
{
public static implicit operator Array(Foo foo)
{
return new int[0]; // doesn't matter
}
static void Main()
{
Foo foo = new Foo();
Array x = (Array)foo; // implicit operator called via compiler
dynamic dyn = foo;
Array y = (Array)dyn; // implicit operator called via dynmic
object obj = foo;
Array z = (Array)obj; // implicit operator NOT called
// - this is a type-check (BOOM!)
}
}
Run Code Online (Sandbox Code Playgroud)