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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T10:34:21+00:00 2026-05-27T10:34:21+00:00

I have a class declared as such: public abstract class CrmAttribute <T> : ICrmAttribute

  • 0

I have a class declared as such:

public abstract class CrmAttribute <T> : ICrmAttribute
{

}

And in one of the methods further down I have this

castedValue = (T)value;

castedValue is then used in other methods of type T as a parameter.

My problem is a case has come up such that the above cast throws an InvalidCastException.

From what I can tell this is one specific case; In Dynamics 2011 Microsoft finally moved away from custom types like CRMNumber and moved to inherent .Net types but not for all cases. Of the ones that remain, is a complex Money type that has one property of .Value which contains a decimal. This is where my Exception is thrown as I cannot cast a decimal to a Money type. Even though a Money object is essentially a Decimal.

I’m still fairly new to generics and much of this is inherited code so I’m well aware the correct response is to not do this per se, But all I want to do is to be able to create my own implicit cast of type Money to Decimal so that the line using T will cast correctly.

Is this possible?

  • 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-27T10:34:22+00:00Added an answer on May 27, 2026 at 10:34 am

    Your best bet is to add code to your class around the cast to test if T is the problematic type and perform the appropriate conversion instead of relying on cast.

    Perhaps something like this:

    if (typeof(Money).IsAssignableFrom(typeof(T))
    {
        // special handling required to convert decimal to money
        castedValue = new Money() { Value = value };  // ? best guess
    }
    else
    {
        castedValue =(T)value;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a class declared as: public class Foo<T> : Panel where T :
I have declared a class as class DCFrameListener : public FrameListener, public OIS::MouseListener, public
WorkAround: I have declared a class level Public static variable and initialized with a
If I have a java class which is package-private (declared with class, not public
I have two classes declared like this: class Object1 { protected ulong guid; protected
I have a relationship between two base classes: public abstract class RecruiterBase<T> { //
I have the following structure: abstract class Base { public abstract List<...> Get(); //What
I have a class named MANAGER public abstract class MANAGER { } Nothing complex
If I have a class defined as such: class classWithInt { public: classWithInt(); ...
Suppose I have a class such as Value defined below. template <typename T> class

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.