Solved

Problem with converting variable to Generic class

Posted on 2016-08-29
11
84 Views
Last Modified: 2016-09-08
I' trying to achieve the following :
I have an interface with an method in it :

---------------------------------------------------------------------
  public interface IDetailPaneUserControl<T>
  {
    T SelectedItem { get; set; }

    Task LoadView(RadPane paneContainer, T item, PaneUserControl parentPane = null);

    Task LoadView(RadPane paneContainer, Guid? id, PaneUserControl parentPane = null);
  }
---------------------------------------------------------------------

The Implementation is like the following :

---------------------------------------------------------------------
 public class DetailPaneUserControl<T> : PaneUserControl, IDetailPaneUserControl<T>, IMayBeClosed where T : class, IBaseEntity, new()
---------------------------------------------------------------------

This class is inherited in my usercontrol.

However I now need to check of a control is of this type and use a method of it. I cannot give with type, but it have to have the IBaseEntity interface.

---------------------------------------------------------------------
 if (item != null)
            {
                var itemType = item.GetType();
                if (paneContent is IDetailPaneUserControl<itemType>)
                    await ((IDetailPaneUserControl<itemType>) paneContent).LoadView(pane, item);
            }
---------------------------------------------------------------------

The item.GetType() isn't working, saying that itemType is a variable but used as an type.

Any idea's how to get the code working?
0
Comment
Question by:MobileAccess
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 3
  • 3
11 Comments
 
LVL 44

Expert Comment

by:AndyAinscow
ID: 41775826
Does changing
var itemType = item.GetType();
to
Type itemType = item.GetType();
cure it?
0
 
LVL 20

Expert Comment

by:Daniel Van Der Werken
ID: 41776176
My guess is that this line:

if (paneContent is IDetailPaneUserControl<itemType>)

is expecting an actual Type in <itemType> and what you're passing in is looked upon as a variable.

If AndyAinscow's solution doesn't fix it, I might try something like:

if (paneContent is IDetailPaneUserControl<Item>)

Open in new window


where
Item 

Open in new window

is the true class name of the itemType object. I don't see that in your code.
0
 

Author Comment

by:MobileAccess
ID: 41779526
The problem is that I cannot code it like that. It needs to be specified at runtime. So I can't place the real name (type) of the item. it needs to be dynamic
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 44

Expert Comment

by:AndyAinscow
ID: 41779538
Do you mean my suggestion doesn't compile and/or run  ?
0
 
LVL 20

Expert Comment

by:Daniel Van Der Werken
ID: 41781718
I think he means mine, because he can't determine the type until run-time.

I'm thinking of using the Dynamic keyword here, but I don't have an example off hand.  It's just a thought and might not really be correct.
0
 
LVL 44

Expert Comment

by:AndyAinscow
ID: 41781734
It would be nice for the asker to respond so we know which isn't working.  Or close the question if one does actually solve it.
Just guessing is going to waste everyones time.
0
 

Author Comment

by:MobileAccess
ID: 41784298
Andy, I'm sorry, I was sick last week...
Your method I've tried, but the problem stays the same.

Daniel,
Indeed, I do not know the type accept at runtime. I'll take a look at the dynamic approach, haven't used this before...
0
 
LVL 20

Expert Comment

by:Daniel Van Der Werken
ID: 41786076
Apparently, this is doable.

This builds. Not promising you it will run!

        Item item = new Item();
        PaneUserControl paneContent = new PaneUserControl();
        RadPane pane = new RadPane();

        if (item != null)
        {
            var genericItemType = item.GetType();
            var specificItemType = genericItemType.MakeGenericType(item.GetType());
            var list = Activator.CreateInstance(specificItemType);
            if (paneContent is IDetailPaneUserControl<Item>)
            {
                ((IDetailPaneUserControl<Item>)paneContent).LoadView(pane, item);
            }
        }

Open in new window

0
 
LVL 20

Expert Comment

by:Daniel Van Der Werken
ID: 41786088
Ugh. I made a mistake. Let me fix it. That does not answer your question.
0
 
LVL 20

Accepted Solution

by:
Daniel Van Der Werken earned 500 total points
ID: 41786283
Okay. We can't get rid of using  if (myDetailPanelUserControl is IDetailPaneUserControl<Item>) with the actual type, Item, being used. It just won't work as far as I can tell.

However, we can "dynamically" generate everything, I think.

        Item item = new Item();
        RadPane pane = new RadPane();

        if (item != null)
        {
            Type genericIDetailPaneUserControlType = typeof(IDetailPaneUserControl<>);
            Type[] typeArgs = { item.GetType() };
            Type[] methodTypeArgs = new Type[] { typeof(RadPane), item.GetType(), typeof(PaneUserControl) };

            Type specificicIDetailPaneUserControlType = genericIDetailPaneUserControlType.MakeGenericType(typeArgs);

            var myDetailPanelUserControl = Activator.CreateInstance(specificicIDetailPaneUserControlType);

            MethodInfo methodInfo = myDetailPanelUserControl.GetType().GetMethod("LoadView").MakeGenericMethod(methodTypeArgs);

            methodInfo.Invoke(this, new object[] { pane, item });

        }

Open in new window

0
 

Author Comment

by:MobileAccess
ID: 41788386
Thanks, I'll try this tomorrow :)
0

Featured Post

The Orion Papers

Are you interested in becoming an AWS Certified Solutions Architect?

Discover a new interactive way of training for the exam.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …

691 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question