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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T09:05:36+00:00 2026-05-29T09:05:36+00:00

I have this extension method for my BusinessObject class: public static class Extensions {

  • 0

I have this extension method for my BusinessObject class:

public static class Extensions
{
    public static T Clone<T>(this T obj)
        where T: BusinessObject<T>, new()
    {
        T newObj = new T();
        var props = newObj.Properties;

        foreach (var p in props)
            newObj.Properties[p.Name].SetValue(newObj, obj.Properties[p.Name].GetValue(obj));

        return newObj;
    }
}

The contents of that method work great, but I have a non-generic BusinessObject class and a generic counterpart. A lot of my code passes around an instance of a generic version of the object inside a non-generic variable (please don’t ask me 20 questions about why). I haven’t had a problem until now because when I call this extension method it expects a generic version.

The code that calls the Clone method uses a BusinessObject variable that contains an instance of a BusinessObject variable. How can I cast the variable to what it actually is? In other words:

Customer cust = new Customer();   // Customer derives from BusinessObject<Customer>
var CustomerClone = cust.Clone(); // This works

BusinessObject obj = cust;
var clone = obj.Clone(); // This doesn't work

Now of course in this example I know that ‘obj’ is a Customer, but in my actual method I don’t know that as it could be any number of derived types. I can find out what the derived type is easily enough by simply using GetType(), but how do I cast the variable to that type at runtime?

  • 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-29T09:05:37+00:00Added an answer on May 29, 2026 at 9:05 am

    In your case, I would say “lose the T” – you aren’t using for anything important that can’t be done in other ways (Activator.CreateInstance, for example). You could offer two APIs – one generic, one non-generic, to allow for convenient casting. For example:

    BusinessObject newObj = (BusinessObject)Activator.CreateInstance(obj.GetType());
    

    It is also quite key because the T you want is not actually the declaration T, but the concrete T. Meaning: if you have a SuperCustomer : Customer, but place it in a Customer variable, and then call Clone, you want to get a new SuperCustomer. T, however, would be Customer. Using GetType() would be far more reliable.

    Another useful trick here is dynamic, which is a sneaky way of flipping from non-generic to generic. Consider:

    dynamic foo = 123;
    Bar(foo);
    
    void Bar<T>(T bar) {
       Console.WriteLine(typeof(T));
    }
    

    will write System.Int32. It has flipped to the correct T on the fly, despite only really knowing about object at the compiler (dynamic is implemented mostly as object, with some fancy bits).

    However, to emphasise – I wouldn’t use that here: I would just have:

    public static BusinessObject Clone(this BusinessObject obj)
    {
        BusinessObject newObj = (BusinessObject)
              Activator.CreateInstance(obj.GetType());
        var props = newObj.Properties;
    
        foreach (var p in props)
            newObj.Properties[p.Name].SetValue(newObj,
              obj.Properties[p.Name].GetValue(obj));
    
        return newObj;
    }
    

    with dynamic as my fallback strategy if I really needed the generics.

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

Sidebar

Related Questions

I have this extension method public static string SerializeObject<T>(this T value) { var serializer
I have this extension method: public static string GetValueFor<TModel, TProperty>(this HtmlHelper<TModel> helper, Expression<Func<TModel, TProperty>>
Let's say I have this jQuery extension method: $.fn.foobar = function() { var clone
This extension method does not work on two separate development machines: public static string
Lets say I have this extention method: public static bool HasFive<T>(this IEnumerable<T> subjects) {
I have the following method: public static TEventInvocatorParameters Until <TEventInvocatorParameters, TEventArgs>(this TEventInvocatorParameters p, Func<TEventArgs,
I try to build extension method for IQuerable like this: public static IQueryable<T> FilterByString<T>(this
Is it possible to rewrite this extension method without the parameter? public static string
The CollectionViewSource.GetDefaultView() method is not in Silverlight 3. In WPF I have this extension
I have this extension method I created and it returns a string of all

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.