Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8089435
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T19:23:41+00:00 2026-06-05T19:23:41+00:00

We have a custom LocalizedString type used in our domain model. We want to

  • 0

We have a custom LocalizedString type used in our domain model. We want to decorate the properties with validation attributes like MaxLength. For this, we added implicit operators to enable casts required by this attribute.

Strangely, the operator seems to never get called and an InvalidCastException get’s thrown in the attributes IsValid method. Performing this cast in our own project works.

Is there a special cast behavior compiler magix going on in this system clr ngen’ed attribute or something?

// 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);
}

Thanks for your help.

Edit

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.
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T19:23:42+00:00Added an answer on June 5, 2026 at 7:23 pm

    Operators of any kind only apply if the type of object is known at compile time. They are not applied “on the fly” to object.

    You could try using dynamic which does do that.

    Examples:

    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!)
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i have created a LocalizedString custom data type for storing / displaying translations using
I have custom classes that I currently instantiate within App.xaml as resources. I want
I have custom exceptions in my django project that look like this: class CustomFooError(Exception):
I have custom cell which contains number of rows in it.i want to write
I have a custom field name front_sl_slider. I want to display all the posts
I have custom buttons in the nav bar, I want to toggle their state
I have custom placeholders in a textarea and I would like every browser to
I have custom button, with an image that I'd like to shift down within
I have custom SPlist with some fields that I'd like to bind to a
I have Custom uitableviewcell: ScrollViewCell I want to know what the difference is between

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.