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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T02:09:17+00:00 2026-06-14T02:09:17+00:00

I am trying to use a class that has a explicit (but also fails

  • 0

I am trying to use a class that has a explicit (but also fails with implicit) cast operator that fails when using LINQ’s Cast<T>() function. Here is the definitions of the two classes

public class DatabaseInfoElement : ConfigurationElement
{
    [ConfigurationProperty("AllowedServer", IsRequired = true)]
    public string AllowedServer { get { return (string)base["AllowedServer"]; } }

    [ConfigurationProperty("DatabaseName", IsRequired = true)]
    public string DatabaseName { get { return (string)base["DatabaseName"]; } }

    [ConfigurationProperty("SqlInstance", IsRequired = true)]
    public string SqlInstance { get { return (string)base["SqlInstance"]; } }

    public static explicit operator DatabaseInfo(DatabaseInfoElement element)
    {
        return new DatabaseInfo(element.AllowedServer, element.DatabaseName, element.SqlInstance);
    }

}

public class DatabaseInfo
{
    public DatabaseInfo(string allowedServer, string sqlInstance, string databaseName)
    {
        AllowedServer = allowedServer;
        SqlInstance = sqlInstance;
        DatabaseName = databaseName;
    }

    public string AllowedServer { get; set; }
    public string SqlInstance { get; set; }
    public string DatabaseName { get; set; }
}

Here is the code I am using to test it.

//Gets the ConfigurationSection that contains the collection "Databases"
var section = DatabaseInfoConfig.GetSection();

//This line works perfectly.
DatabaseInfo test = (DatabaseInfo)section.Databases[0];

//This line throws a execption
var test2 = new List<DatabaseInfo>(section.Databases.Cast<DatabaseInfo>());

Here is the exception I get

System.InvalidCastException was unhandled by user code
  HResult=-2147467262
  Message=Unable to cast object of type 'Server.Config.DatabaseInfoElement' to type 'Server.DatabaseInfo'.
  Source=System.Core
  StackTrace:
       at System.Linq.Enumerable.d__b1`1.MoveNext()
       at System.Collections.Generic.List`1..ctor(IEnumerable`1 collection)
       at Sandbox.Main() in E:\Code\Sandbox\Program.cs:line 82
  InnerException: 

What am I doing wrong in my casting to get this to work the way I want?

  • 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-14T02:09:19+00:00Added an answer on June 14, 2026 at 2:09 am

    When you define explicit/implicit cast operators, they are bound at call-sites at compile-time. That’s why the first line works: the compiler can work out all the type information needed, and so it can substitute your custom explicit cast operator for the default one.

    However, since the Cast<T> just performs a generic cast, the compiler doesn’t know about your operator, and thus it is ignored. Result: invalid cast exception.

    You can get around this by instead performing a .Select(x => (DatabaseInfo)x). Alternatively, you could add on a method called ToDatabaseInfo(), so that you’re not hiding what’s actually going on.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to write a class that has a generic member variable but is
I'm trying to use Dynamic Data with a entity model that has Abstract class
I'm trying to use a generator with a Python class that works somewhat similarly
I have a third-party class that I am trying to use in Hadoop, and
I'm trying to build a utility class that I can re-use, that converts a
I've been trying to write an error handling class that I can use on
I'm trying to use SFINAE to detect if a class has an overloaded member
I'm trying to use the MoqAutoMocker class that comes with StructureMap and I can't
I am trying to create a regular expression with a character class that has
I am trying to use an AsyncTask class that I created to update a

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.