Solved

Generic interface Return type

Posted on 2013-05-17
2
174 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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

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…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

705 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now