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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T01:11:28+00:00 2026-06-09T01:11:28+00:00

Is it possible to get the type of object and dynamically set it as

  • 0

Is it possible to get the type of object and dynamically set it as the object. I have several ViewModels that all contain the same Property and want to do something like this

if (this.DataContext is CamerasViewModel)
{
     //Type type = Type.GetType((this.DataContext.ToString());
     object o = Assembly.GetExecutingAssembly().CreateInstance(this.DataContext.GetType().ToString());
     Type type = o.GetType();

     foreach (ButtonViewModel button in (this.DataContext as type).Buttons)
     {
         if (button.DisplayName == this.Content.ToString())
         {
             this.Template = (ControlTemplate)this.FindResource(button.TemplateResource.Substring(0, button.TemplateResource.Length - 3) + "pr");
             break;
         }
     }
 }

Instead of saying this.DataContext as CamerasViewModel I want to say this.DataContext as THEDYNAMICTYPE

Any suggestions?

  • 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-09T01:11:30+00:00Added an answer on June 9, 2026 at 1:11 am

    You’re probably much better taking all of your classes that have a Buttons property that you want to update the template on, and having them implement an interface. The interface would look something like this:

    public interface IHasButtons
    {
      public IEnumerable<ButtonViewModel>  Buttons {get; set;}
    }
    

    and your view models would be declared something like this:

    public class CamerasViewModel : IHasButtons
    {
      public IEnumerable<ButtonViewModel> Buttons {get {. . .} set {. . .} }
      . . .
    }
    

    Then, in your if statement, instead of checking if the object is a CamerasViewModel, check if it’s an IHasButtons. Doing it this way is a lot safer than trying to determine at runtime if there’s a Buttons property on the object. You could get unlucky and run across a Buttons property that has the same name, but different functionality than you’re expecting. Then you’re back writing crazy logic to determine if this is REALLY the Buttons collection that you’re looking for. If you do it with interfaces, it’s extremely clear. If a ViewModel implements IHasButtons, then it’s a ViewModel you want to update. If it doesn’t implement IHasButtons, you’ll skip right over it

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

Sidebar

Related Questions

Is it possible in JSP to get the type of Object in List, just
I'm currently trying to get the type that an object was casted as in
I am trying to see if it possible to get object type for a
I have an object to deserialize but the object has custom type ApplicationLanguage that
Is it possible to get the parent object from a child object dynamically? Essentially,
is it possible to change type of whole array for example, I have: object[]
I've got a type_info object that defines type of property in my property map.
Possible Duplicate: PHP detecting request type (GET, POST, PUT or DELETE) This should be
Possible Duplicate: Default value of a type In C#, to get the default value
I have a form that is generated based on several DataTemplate elements. One of

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.