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 7526321
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T03:47:48+00:00 2026-05-30T03:47:48+00:00

Using MVC3 and the StringLength validation attribute on a property of type MyType<string> who’s

  • 0

Using MVC3 and the StringLength validation attribute on a property of type MyType<string> who’s internal value is a string, I get an exception:

Unable to cast object of type ‘MyType’ to type ‘System.String’.

So I added an operator overload, here’s my class:

public class MyType<T>
{
    public T Value { get; set; }
    //...

    public static implicit operator string(MyType<T> instance)
    {
        //Return the internal value of the instance.
        return Convert.ToString(instance.Value);
    }
}

So this should, in theory, allow MyType to be cast to a String. However, I still get the same error, here is the stack trace:

[InvalidCastException: Unable to cast object of type ‘MyType’ to type
‘System.String’.]
System.ComponentModel.DataAnnotations.StringLengthAttribute.IsValid(Object
value) +64
System.ComponentModel.DataAnnotations.ValidationAttribute.IsValid(Object
value, ValidationContext validationContext) +176
System.ComponentModel.DataAnnotations.ValidationAttribute.GetValidationResult(Object
value, ValidationContext validationContext) +41
System.Web.Mvc.d__1.MoveNext() +267

The StringLengthAttribute.IsValid method (from the .NET framework, not my code) is doing this:

public override bool IsValid(object value)
{
    this.EnsureLegalLengths();
    int num = (value == null) ? 0 : ((string)value).Length;
    return value == null || (num >= this.MinimumLength && num <= this.MaximumLength);
}

Seems like it should work, what am I missing?

  • 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-05-30T03:47:49+00:00Added an answer on May 30, 2026 at 3:47 am

    The variable value is declared of type object. You must first cast to the actual type first since the underlying object is not a string. There are no conversions defined from object to other types so the cast you’re doing is effectively telling the compiler that this object is actually a string when it is not. This is essentially the same kind of rule that must be followed when unboxing value types.

    By casting it to your type first, the compiler can then realize that a (implicit) conversion exists from your type to the desired string type and can generate the appropriate instructions.

    e.g.,

    object obj = new MyObject<string> { Value = "Foobar" };
    string x = (string)obj;           // fail: obj is not a string
    string y = (MyObject<string>)obj; // obj: obj is cast to MyObject<string> and
                                      //      an implicit cast to string exists
    // to be clearer
    string z = (string)(MyObject<string>)obj;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to get client validation working in MVC3 using data annotations. I
Using MVC3 and Entity Framework. Am trying to get validation flowing from data model
I'm using MVC3 - i have a javascript function that uses jQuery get() to
Using MVC3 I have found that setting a SelectList's selected value correctly renders the
I just created a sample MVC3 application to learn validation. It is using DataAnnotations.
using MVC3, Code First (DbContext) Is there a way to implement (ideally) a facade
I'm using MVC3 with C# code. I have a table in my SQL Server
I'm using MVC3 with EF (version 4, but not sure if 4.0,4.1,etc). I've been
So I'm using MVC3 and I am aware of the following code to return
I'm using MVC3 razor, and I'm trying to pass an object to a partial

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.