Solved

Generic interface Return type

Posted on 2013-05-17
2
186 Views
Last Modified: 2014-03-28
Hi ,

I have a interface

public interface IMyInterface<T>
{
long Method(T obj);
}

My Base class is

public abstract class MyClass<T>:IMyInterface<T>
{
public abstract long Method(T Obj);
}

public class Class1
{
public string val1{get;set;}
public string val2{get;set;}
}

public class Class2
{
public string val3{get;set;}
public string val4{get;set;}

}

My implementation class would be

Public class MyderivedClass1:MyClass<Class1>
{
public override long Method(Class1 Obj)
{
             /// Method implementation
}
}

Public class MyderivedClass2:MyClass<Class2>
{
public override long Method(Class2 Obj)
{
             /// Method implementation
}
}


I have created a new method which would give the interface intance based on the input string

static IMyInterface<T> getintance<T>(string name)
{
if(name=="Class1")
{
IMyInterface<Class1> instance = new MyderivedClass1();
                    return (IMyInterface<T>)instance ;
}
else if (name=="Class1")
{
IMyInterface<Class2> instance = new MyderivedClass2();
                    return (IMyInterface<T>)instance ;
}

}


In my calling method i need to specify the particular class type to get the intance .

getintance<Class1>("Class1");
or
getintance<Class2>("Class2");

already i'm passing the string to identify the intance.

I need to get the intance without passing the class type.

Any help one this .
0
Comment
Question by:selva_kongu
2 Comments
 
LVL 10

Expert Comment

by:eguilherme
ID: 39174552
If you need the Interface anom to be typed you will need to pass the instance, otherwise the compiler wont know what it is, unless you work with dynamics but you lose intellisense with that
0
 
LVL 15

Accepted Solution

by:
Minh Võ Công earned 500 total points
ID: 39253432
static IMyInterface<T> getintance<T>()
{
string name = typeof(T);
if(name=="Class1")
{
IMyInterface<Class1> instance = new MyderivedClass1();
                    return (IMyInterface<T>)instance ;
}
else if (name=="Class1")
{
IMyInterface<Class2> instance = new MyderivedClass2();
                    return (IMyInterface<T>)instance ;
}

}

http://stackoverflow.com/questions/325156/calling-generic-method-with-a-type-argument-known-only-at-execution-time
0

Featured Post

Technology Partners: 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!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
C# MVC Insert Multiple Row into DB 2 60
How to repeat the data 4 30
Adding where clause to entity statement c# 1 46
Need help with another query 10 34
We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

679 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