?
Solved

Generic interface Return type

Posted on 2013-05-17
2
Medium Priority
?
191 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
[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
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 1500 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…

719 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