P
Peter Morris
Hi all
Is there any way I can remove the non-generic IView interface and work only
with IView<T>? The PassReferences method will only receive a System.Object
instance so I wont know the type of the object I need a view for until
runtime. I'd really like to be able to remove the non-generic IView
interface so that anything implementing IView<T> doesn't also need a weakly
typed method which simply typecasts and passes onto the IView<T> interface's
method.
Thanks
using System;
using System.Collections.Generic;
using System.Text;
namespace ConsoleApplication17
{
public interface IView
{
void DoSomething(object viewObject);
}
public interface IView<T> : IView
{
void DoSomething(T viewObject);
}
public class PersonDto
{
public string Name { get; set; }
}
public class PersonDtoView : IView<PersonDto>
{
public void DoSomething(object viewObject)
{
var personDto = (PersonDto)viewObject;
DoSomething(personDto);
}
public void DoSomething(PersonDto viewObject)
{
Console.WriteLine(viewObject.Name);
}
}
class Program
{
static void Main(string[] args)
{
var personDto = new PersonDto();
personDto.Name = "Peter Morris";
var view = new PersonDtoView();
view.DoSomething(personDto);
PassReferences(personDto);
}
static void PassReferences(object viewObject)
{
//Determine the view type
Type viewTypeRequired = typeof(IView<>).MakeGenericType(new Type[] {
viewObject.GetType() });
//Use some mechanism to get the implementor for the required view type
//not relevant here so I will hard code it, would be something like:
//Type viewTypeImplementor = ViewTypeImplementors[viewTypeRequired];
Type viewTypeImplementor = typeof(PersonDtoView);
//Create an instance of the view implementor
var view = (IView)Activator.CreateInstance(viewTypeImplementor);
//Execute the view
view.DoSomething(viewObject);
}
}
}
Is there any way I can remove the non-generic IView interface and work only
with IView<T>? The PassReferences method will only receive a System.Object
instance so I wont know the type of the object I need a view for until
runtime. I'd really like to be able to remove the non-generic IView
interface so that anything implementing IView<T> doesn't also need a weakly
typed method which simply typecasts and passes onto the IView<T> interface's
method.
Thanks
using System;
using System.Collections.Generic;
using System.Text;
namespace ConsoleApplication17
{
public interface IView
{
void DoSomething(object viewObject);
}
public interface IView<T> : IView
{
void DoSomething(T viewObject);
}
public class PersonDto
{
public string Name { get; set; }
}
public class PersonDtoView : IView<PersonDto>
{
public void DoSomething(object viewObject)
{
var personDto = (PersonDto)viewObject;
DoSomething(personDto);
}
public void DoSomething(PersonDto viewObject)
{
Console.WriteLine(viewObject.Name);
}
}
class Program
{
static void Main(string[] args)
{
var personDto = new PersonDto();
personDto.Name = "Peter Morris";
var view = new PersonDtoView();
view.DoSomething(personDto);
PassReferences(personDto);
}
static void PassReferences(object viewObject)
{
//Determine the view type
Type viewTypeRequired = typeof(IView<>).MakeGenericType(new Type[] {
viewObject.GetType() });
//Use some mechanism to get the implementor for the required view type
//not relevant here so I will hard code it, would be something like:
//Type viewTypeImplementor = ViewTypeImplementors[viewTypeRequired];
Type viewTypeImplementor = typeof(PersonDtoView);
//Create an instance of the view implementor
var view = (IView)Activator.CreateInstance(viewTypeImplementor);
//Execute the view
view.DoSomething(viewObject);
}
}
}