MobileAccess
asked on
Problem with converting variable to Generic class
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<ite mType>)
await ((IDetailPaneUserControl<i temType>) 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?
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>,
--------------------------
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<ite
await ((IDetailPaneUserControl<i
}
--------------------------
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?
My guess is that this line:
if (paneContent is IDetailPaneUserControl<ite mType>)
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:
where
if (paneContent is IDetailPaneUserControl<ite
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>)
where
Item
is the true class name of the itemType object. I don't see that in your code.
ASKER
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
Do you mean my suggestion doesn't compile and/or run ?
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.
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.
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.
Just guessing is going to waste everyones time.
ASKER
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...
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...
Apparently, this is doable.
This builds. Not promising you it will run!
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);
}
}
Ugh. I made a mistake. Let me fix it. That does not answer your question.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks, I'll try this tomorrow :)
var itemType = item.GetType();
to
Type itemType = item.GetType();
cure it?