How to generalize a class inheriting from another class - c#

I have a class that looks like:
public class InvokeProxy : MarshalRefByObject, IFace
{
public InvokeProxy(IFace face)
{
this.Face = face;
}
private IFace Face { get; set; }
public string Execute(string data)
{
return this.Face.Execute(data)
}
}
And I'm tasked with making it generic. Since I can't inherit from the generic class, I'm somewhat stuck, does anyone know a workaround?

I'm not really sure what you're looking to do by making InvokeProxy into InvokeProxy<T>...does this help?
public class InvokeProxy<T> : MarshalRefByObject, IFace where T : IFace
{
public InvokeProxy(T face)
{
this.Face = face;
}
private T Face { get; set; }
public string Execute(string data)
{
return this.Face.Execute(data);
}
}

Not really sure if I understood the question....
public class InvokeProxy<T> : MarshalRefByObject where T : class
{
public InvokeProxy(T face)
{
this.Face = face;
}
private T Face { get; set; }
public string Execute(string data)
{
return this.Face.Execute(data)
}
}

Related

Implement an interface method with a concrete class

I have the following interfaces, one for the entity and one for some logic:
public interface IItem
{
int Id { get; set; }
}
public interface IGenerator
{
IList<IItem> Generate();
}
and implementation:
public class ItemA : IItem
{
public int Id { get; set; }
public string Name { get; set; }
}
public class ItemAGenerator : IGenerator
{
public IList<ItemA> Generate()
{
// do stuff
return List<ItemA>;
}
}
That implementation did not work, it says that it does not have the matching return type, so I also tried:
public class ItemAGenerator : IGenerator
{
public IList<IItem> Generate()
{
// do stuff
return List<ItemA>;
}
}
it does not work as well, it says: cannot implicitly convert type List<IItem> to List<ItemA>.
How to make it work? what am I missing here.
Just create the list as a List<IItem> but add ItemA's to it.
public class ItemAGenerator : IGenerator
{
public IList<IItem> Generate()
{
var list = new List<IItem>();
list.Add(new ItemA());
return list;
}
}
Just make IGenerator generic. Then you can specify the type that will be returned.
public interface IGenerator<T> where T : IItem
{
IList<T> Generate();
}
public class ItemAGenerator : IGenerator<ItemA>
{
public IList<ItemA> Generate()
{
// do stuff
return List<ItemA>;
}
}

Generic Abstract Class

I have the following code which is fine...
namespace GenericAbstract
{
public interface INotifModel
{
string Data { get; set; }
}
public interface INotif<T> where T: INotifModel
{
T Model { get; set; }
}
public interface INotifProcessor<in T> where T : INotif<INotifModel>
{
void Yell(T notif);
}
public class HelloWorldModel : INotifModel
{
public string Data { get; set; }
public HelloWorldModel()
{
Data = "Hello world!";
}
}
public class HelloWorldNotif : INotif<HelloWorldModel>
{
public HelloWorldModel Model { get; set; }
public HelloWorldNotif()
{
Model = new HelloWorldModel();
}
}
public class HelloWorldProcessor<T> : INotifProcessor<T> where T : INotif<INotifModel>
{
public void Yell(T notif)
{
throw new NotImplementedException();
}
}
}
As you can see there are 3 interfaces and each of those is implemented.
However, I would like the processor to be implemented like this:
public class HelloWorldProcessor : INotifProcessor<HelloWorldNotif<HelloWorldModel>>
{
public void Yell(HelloWorldNotif<HelloWorldModel> notif)
{
throw new NotImplementedException();
}
}
But i get the following error:
The non-generic type 'HelloWorldNotif' cannot be used with type arguments
I want the HelloWorldProcessor to implement INotifProcessor only for HelloWorldNotif...
Can't figure out what I am doing wrong..
For this to work you first have to make INotif<T> co-variant. That means that the Model property has to be read only for the interface (it can still have a public set in an implementation). Then to fix your immediate error you don't put the <HelloWorldModel> after HelloWorldNotif because it's already a INotif<HelloWorldModel>
public interface INotifModel
{
string Data { get; set; }
}
public interface INotif<out T> where T : INotifModel
{
T Model { get; }
}
public interface INotifProcessor<in T> where T : INotif<INotifModel>
{
void Yell(T notif);
}
public class HelloWorldModel : INotifModel
{
public string Data { get; set; }
public HelloWorldModel()
{
Data = "Hello world!";
}
}
public class HelloWorldNotif : INotif<HelloWorldModel>
{
public HelloWorldModel Model { get; set; }
public HelloWorldNotif()
{
Model = new HelloWorldModel();
}
}
public class HelloWorldProcessor<T> : INotifProcessor<T> where T : INotif<INotifModel>
{
public void Yell(T notif)
{
throw new NotImplementedException();
}
}
public class HelloWorldProcessor : INotifProcessor<HelloWorldNotif>
{
public void Yell(HelloWorldNotif notif)
{
throw new NotImplementedException();
}
}
Then I guess your implementation would be something like
Console.WriteLine(notif.Model.Data);
As others have said and/or implied out you've already got HelloWorldNotif fully specified. So to translate this:
I want the HelloWorldProcessor to implement INotifProcessor only for
HelloWorldNotif
To C#, I think you mean:
public class HelloWorldProcessor : INotifProcessor<HelloWorldNotif>
{
public void Yell(HelloWorldNotif notif)
{
throw new NotImplementedException();
}
}

Generic Abstract Method

I ran into trouble when trying to create an abstract class and a method in it that was generic in nature.
class GameRoomManager : MonoBehaviour {
public GameRoom GetSomething(string s){
//do things here
return GameRoomvar;
}
}
Now I have another class that does something similar, but different classes involved
class PlayerManager : MonoBehaviour{
public Player GetSomething(string s){
//player related things here
return Playervar;
}
}
I want to have both classes GameRoomManager and PlayerManager inherit from an abstract class Abs
class GameRoomManager : Abs{
public override GameRoom GetSomething<GameRoom>(string s){
return GameRoomvar;
}
}
where
public abstract class Abs{
public T GetSomething<T>(string s);
}
I've seen a few answers on this topic when I was looking for solutions, and all suggested the abstract class itself be generic. I don't want to make the abstract class generic, since examples I saw would have me do class GameRoomManager : Abs<GameRoomManager>. But I want the method to return type GameRoom, not GameRoomManager.
I'm not totally familiar with generics, so please point me in the right direction if I'm going wrong
You have to have something in common with PQR and HIJ for the classes to use a common method.
Plan A
Connect things with interfaces.
public interface IPart
{
// put things here that are common between Part and GameRoom
int ID { get; }
}
public interface IAbs
{
IPart GetSomething(string name);
}
public class GameRoom : IPart
{
public int ID { get; set; }
}
public class GameRoomManager : IAbs
{
GameRoom part;
#region IAbs Members
public GameRoom GetSomething(string name)
{
return part;
}
IPart IAbs.GetSomething(string name)
{
return GetSomething(name);
}
#endregion
}
public class Player : IPart
{
public int ID { get; set; }
}
public class PlayerManager : IAbs
{
Player part;
#region IAbs Members
public Player GetSomething(string name)
{
return part;
}
IPart IAbs.GetSomething(string name)
{
return GetSomething(name);
}
#endregion
}
Plan B
Use a base class with a generic type & interfaces
public interface IItem
{
// put things here that are common between Part and GameRoom
int ID { get; }
}
public class GameRoom : IItem
{
public int ID { get; set; }
}
public class Player : IItem
{
public int ID { get; set; }
}
public interface IAbs
{
IItem GetItem(string guid);
}
public abstract class Abs<T> : IAbs
where T : IItem
{
protected abstract T GetItem(string name);
protected Abs(T item)
{
this.Item=item;
}
protected T Item { get; private set; }
#region IAbs Members
IItem IAbs.GetItem(string name)
{
return GetItem(name);
}
#endregion
}
public class GameRoomManager : Abs<GameRoom>
{
public GameRoomManager(GameRoom room) : base(room)
{
}
protected override GameRoom GetItem(string guid)
{
return Item;
}
public GameRoom GetRoom(string guid) { return GetItem(guid); }
}
public class PlayerManager : Abs<Player>
{
public PlayerManager(Player player)
: base(player)
{
}
protected override Player GetItem(string guid)
{
return Item;
}
public Player GetPlayer(string guid) { return GetItem(guid); }
}
here is some example usage:
class Program
{
static void Main(string[] args)
{
List<IAbs> managers=new List<IAbs>();
var pm=new PlayerManager(new Player() { ID=1001 });
var gm=new GameRoomManager(new GameRoom() { ID=2050 });
managers.Add(pm);
managers.Add(gm);
IItem part = managers[0].GetItem("0000");
}
}

Implementing interface properties in interfaces

I need to create a dll file which contains all the interfaces of the classes but doesn't contain any class.
Because I use these interfaces for a lot of classes it's must be like that:
public interface IClassA
{
string Word { get; }
}
public interface ITest<TClassA> where TClassA : IClassA
{
TClassA A { get; }
}
Example of two classes that implements these interfaces the way I want:
public class ClassA : IClassA
{
public string Word
{
get;
private set;
}
public string Sentence
{
get;
private set;
}
public ClassA(string word, string sentence)
{
this.Word = word;
this.Sentence = sentence;
}
}
public class Test : ITest<ClassA>
{
public ClassA A
{
get;
private set;
}
public Test(ClassA a)
{
this.A = a;
}
}
And I want to do something like that in the main program:
public static void Main(string[] args)
{
ClassA a = new ClassA("hey", "hey world!");
Test t = new Test(a);
Print((ITest<IClassA>)t);
}
public static void Print(ITest<IClassA> t)
{
Console.WriteLine(t.A.Word);
}
But this casting: (ITest<IClassA>)t makes a run time error.
How can I solve it?
thanks!
You should declare Test as
public class Test : ITest<IClassA>
instead of ITest<ClassA>.
Or declare ITest<TClassA> to be covariant on TClassA:
public interface ITest<out TClassA> where TClassA : IClassA
The Test-class implements the concrete ClassA (public class Test : ITest<ClassA>).
So you're trying to cast an ITest<ClassA> to ITest<IClassA> which obviously fails.
If you let the Test-class implement IClassA, the cast works:
public class Test : ITest<IClassA>
{
public IClassA A
{
get; private set;
}
public Test(IClassA a)
{
this.A = a;
}
}

what kind of factory pattern to implement when methods accept different signatures?

How do I define a factory whose implementations may accept different numbers of parameters?
public abstract class CarFactory
{
public abstract void countStuff(??); //not sure how to define this
}
I would like the factory to be able to create different objects like:
public class BMW : CarFactory
{
public override void countStuff(param1, param2) {}
}
public class Ford : CarFactory
{
public override void countStuff(param1) {}
}
Not sure if "countStuff" should be a factory responsibility, but you could get something similar this way:
public interface ICountParam {}
public class BmwParam : ICountParam
{
public BmwParam(string a)
{
A = a;
}
public string A { get; set; }
}
public class FordParam : ICountParam
{
public FordParam(string a, string b)
{
A = a;
B = b;
}
public string A { get; set; }
public string B { get; set; }
}
public interface ICarFactory<in T> where T : ICountParam
{
void CountStuff(T param);
}
public class BMW : ICarFactory<BmwParam>
{
public void CountStuff(BmwParam param) { }
}
public class Ford : ICarFactory<FordParam>
{
public void CountStuff(FordParam param) { }
}
Usage:
bmw.CountStuff(new BmwParam("A"));
ford.CountStuff(new FordParam("A", "B"));

Categories

Resources