Сообщение Re: Сделал так от 31.07.2020 8:24
Изменено 31.07.2020 8:40 igor-booch
Re: Сделал так
Всем спасибо, c рефлешеном, понятно, что можно всё, но производительность страдает,
пока сделал так
пока сделал так
using System;
namespace ConsoleApp17
{
class Program
{
static void Main(string[] args)
{
Class @object = new Class();
IBase @base = @object;
IDerived1 derived1 = @object;
IDerived2 derived2 = @object;
@base.Do();
derived1.Do();
derived2.Do();
Do<IBase>(@base);
Do<IDerived1>(derived1);
Do<IDerived2>(derived2);
Console.ReadLine();
}
public static void Do<TClass>(TClass @object)
where TClass : IBase
{
if (typeof(TClass) == typeof(IDerived1))
((IDerived1) @object).Do();
else if (typeof(TClass) == typeof(IDerived2))
((IDerived2) @object).Do();
else
((IBase) @object).Do();
}
}
public class Class : IDerived1, IDerived2
{
#region Implementation of IBase
void IBase.Do()
{
Console.WriteLine("IBase");
}
void IDerived1.Do()
{
Console.WriteLine("IDerived1");
}
void IDerived2.Do()
{
Console.WriteLine("IDerived2");
}
#endregion
}
public interface IBase
{
void Do();
}
public interface IDerived1 : IBase
{
new void Do();
}
public interface IDerived2 : IBase
{
new void Do();
}
}
Re: Сделал так
Всем спасибо, c рефлешеном, понятно, что можно всё, но производительность страдает,
пока сделал так
пока сделал так
using System;
namespace ConsoleApp17
{
class Program
{
static void Main(string[] args)
{
Class @object = new Class();
IBase @base = @object;
IDerived1 derived1 = @object;
IDerived2 derived2 = @object;
@base.Do();
derived1.Do();
derived2.Do();
Do(@base);
Do(derived1);
Do(derived2);
Console.ReadLine();
}
public static void Do<TClass>(TClass @object)
where TClass : IBase
{
if (typeof(TClass) == typeof(IDerived1))
((IDerived1) @object).Do();
else if (typeof(TClass) == typeof(IDerived2))
((IDerived2) @object).Do();
else
((IBase) @object).Do();
}
}
public class Class : IDerived1, IDerived2
{
#region Implementation of IBase
void IBase.Do()
{
Console.WriteLine("IBase");
}
void IDerived1.Do()
{
Console.WriteLine("IDerived1");
}
void IDerived2.Do()
{
Console.WriteLine("IDerived2");
}
#endregion
}
public interface IBase
{
void Do();
}
public interface IDerived1 : IBase
{
new void Do();
}
public interface IDerived2 : IBase
{
new void Do();
}
}