Solved

Generic interface Return type

Posted on 2013-05-17
2
189 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 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 Install Bash on Windows 10

Windows’ budding partnership with Canonical has certainly led to some great improvements. One of them being the ability to use Bash on your Windows machine without third party applications! This might be one of the greatest things a cloud engineer in a Windows environment can do!

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…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…

631 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