I want to do something like this:
public class Object1 {
public int number{get;set;}
}
public class Object2 {
public int number{get;set;}
}
public class Object3 {
public int number{get;set;}
}
main();
public void main(){
var objects1 = new List<Object1>{new Object1{number=1} , new Object1{number=2}};
test<Object1>(objects1);
}
public List<Object3> test<T>(IEnumerable<T> objs){
var rv = new List<Object3>();
foreach (var o in objs)
{
var foo = overloaded(o);
rv.Add(foo);
}
return rv;
}
public Object3 overloaded(Object1 obj){
// Run very specific things to Object1
return new Object3{number=obj.number+1};
}
public Object3 overloaded(Object2 obj){
// Run very specific things to Object2
return new Object3{number=obj.number+2};
}
You can directly run/edit the code here, with error handling:
http://csharppad.com/gist/6ff5f13cac8f0e5735be
The error I get is Argument 1: cannot convert from 'T' to 'Object1' - So how can I do this? The idea is that Object1 and Object2 have 95% of their code identical, it's that last 5% that I need to have it do something specific for each.
You could use dynamic to in your test method, just note that there are performance implications:
overloaded((dynamic)obj);
I would reverse your thinking.
Try this:
private void test<T>(T obj){
// Do common stuff
}
public void overloaded(Object1 obj){
test(obj);
Console.WriteLine("Do Object 1 stuff");
}
public void overloaded(Object2 obj){
test(obj);
Console.WriteLine("Do Object 2 stuff");
}
and call overloaded instead of calling test.
After playing around I was able to come up with this
public class Object1 {
public int number{get;set;}
}
public class Object2 {
public int number{get;set;}
}
public class Object3 {
public int number{get;set;}
}
main();
public void main(){
var objects1 = new List<Object1>{new Object1{number=1} , new Object1{number=2}};
test<Object1>(objects1);
}
public List<Object3> test<T>(IEnumerable<T> objs){
var rv = new List<Object3>();
foreach (var o in objs)
{
if(typeof(T) == typeof(Object1)){
rv.Add(overloaded((Object1)(object)o));
} else {
rv.Add(overloaded((Object2)(object)o));
}
}
return rv;
}
public Object3 overloaded(Object1 obj){
return new Object3{number=obj.number+1};
}
public Object3 overloaded(Object2 obj){
return new Object3{number=obj.number+2};
}
This works, but seems hacky to me. Wondering what the best way is!
Dependency Injection
interface IObject
{
int number {get;set;}
}
public class Object1 : IObject {
public int number{get;set;}
}
public class Object2 : IObject {
public int number{get;set;}
}
public class Object3 : IObject {
public int number{get;set;}
}
public IObject overloaded(IObject obj){
// Run very specific things to Object1
return new IObject {number=obj.number+1};
}
Related
I am having an issue with Reflection, which I can't seem to find a solution for.
I have the following simple interface :
public interface IDataProperty<T>
{
public T Value { get; set; }
public int BytesCount();
public byte[] Serialize();
}
the struct which implements the interface above :
public struct IntProperty : IDataPropery<int>
{
private int _value;
public int Value { get => _value; set => _value = value; }
public int BytesCount()
{
return 4;
}
public byte[] Serialize()
{
return BitConverter.GetBytes(_value);
}
public IntDataProperty(int value) { _value = value; }
}
and a simple class to hold the values :
public class ValuesContainer
{
public IntProperty prop1;
public IntProperty prop2;
}
I am trying to call the Serialize() method on both prop1 and prop2 in my Processor class,
with no luck so far... :
public class Processor
{
public void ProccesData<T>(out T result) where T : ValuesContainer, new()
{
result = new T();
List<FieldInfo> dataFields = new List<FieldInfo>();
result.GetType().GetFields().ToList().ForEach(field => {
if(field.FieldType.GetInterfaces().Any(i => i.IsGenericType && i.GetGenericTypeDefinition() == typeof(IDataProperty<>)))
{
dataFields.Add(field);
}
});
MethodInfo serializeMI = typeof(IDataProperty<>).GetMethod("Serialize");
foreach(FieldInfo field in dataFields)
{
Console.WriteLine(field.Name);
serializeMI.Invoke(field,null);
}
}
}
Running the code at this point gives me the following error :
'Late bound operations cannot be performed on types or methods for which ContainsGenericParameters is true.'
I am aware that I need to get somehow to the instance behind the field variable, but have no idea how to do it.
Does anyone know a good way of doing what i am trying to achieve using other methods, or only Reflection is the way to go, and if the latter - what solutions do I have ?
Thanks in advance to all of you.
After the great help #JeroenMostert provided in the comments , i have been able to solve the issue at its roots. As long as Jeroen haven't provided an answer to be accepted as correct , I will just show his solution , so this question gets marked as answered. Still, the benefit here goes to #JeroenMostert
internal class Program
{
static void Main(string[] args)
{
ValuesContainer container = new ValuesContainer();
Processor processor = new Processor();
processor.ProccesData(out container);
}
}
public interface IDataProperty<T>
{
public void Serialize();
}
public struct IntProperty : IDataProperty<int>
{
private int _value;
public int Value { get => _value; set => _value = value; }
public int BytesCount()
{
return 4;
}
public void Serialize()
{
Console.WriteLine("I have been invoked !" + this.GetHashCode());
}
}
public class ValuesContainer
{
public IntProperty prop1;
public IntProperty prop2;
}
public class Processor
{
public void ProccesData<T>(out T result) where T : ValuesContainer, new()
{
result = new T();
List<FieldInfo> dataFields = new List<FieldInfo>();
result.GetType().GetFields().ToList().ForEach(field => {
if (field.FieldType.GetInterfaces().Any(i => i.IsGenericType && i.GetGenericTypeDefinition() == typeof(IDataProperty<>)))
{
dataFields.Add(field);
}
});
foreach (FieldInfo field in dataFields)
{
Console.WriteLine("Invoking 'Serialize' method on fieldName :" + field.Name);
field.GetValue(result).GetType().GetMethod("Serialize").Invoke(field.GetValue(result), null);
}
}
}
I need to pass type as an argument to a generic class. I am trying to get the type from list of types. Example:
void Main()
{
var test = new Test();
test.testMethod();
}
public static class ListClass<T>
{
public static bool getValues()
{
return true;
}
}
public class X { public int a; public int b; }
public class Y { public string s; public float f; }
class Test
{
List<Type> listType = new List<Type>();
public Test()
{
listType.Add(typeof(X));
listType.Add(typeof(Y));
}
public void testMethod()
{
Console.WriteLine(ListClass<X>.getValues());
Console.WriteLine(ListClass<Y>.getValues());
}
}
I want to loop the calls instead of calling in each line.
Let's assume, for the sake of the argument, that the code you posted in your question, as per my request in the comments, was this:
void Main()
{
var test = new Test();
test.testMethod();
}
public static class ListClass<T>
{
public static bool getValues()
{
return true;
}
}
public class X { public int a; public int b; }
public class Y { public string s; public float f; }
class Test
{
List<Type> listType = new List<Type>();
public Test()
{
listType.Add(typeof(X));
listType.Add(typeof(Y));
}
public void testMethod()
{
Console.WriteLine(ListClass<X>.getValues());
Console.WriteLine(ListClass<Y>.getValues());
}
}
That's basically code that will compile and will run. So now you want to know how to actually run this illegal code:
public void testMethod()
{
foreach (var type in listType)
{
Console.WriteLine(ListClass<type>.getValues());
}
}
Here's how:
public void testMethod()
{
foreach (var type in listType)
{
Console.WriteLine(
(bool)typeof(ListClass<>)
.MakeGenericType(type)
.GetMethod("getValues")
.Invoke(null, new object[] { }));
}
}
Now I don't know if this is the code you need because you didn't post the example that I was asking for. Nevertheless, I hope this helps.
I have following code:
public class A
{
public int MyProperty {get; set;}
}
public class B
{
A myInstance = new A();
myInstance.MyProperty = 10;
}
public class C
{
public void InvokeA()
{
//How to access MyPropery here?
BInstance = new B();
Console.WriteLine(B.myInstance.MyProperty.ToString());
}
}
I'm looking for a way to access MyProperty as written above. Inheritance is not an option since my class C is already inherited from some base class. A way without declaring any of the given classes as static would be nice!
Thanks,
Orz
Consider following classes:
public class A
{
public int MyProperty { get; set; }
}
public class B
{
public A GetAInstance()
{
A myInstance = new A();
myInstance.MyProperty = 10;
return myInstance;
}
}
public class C
{
private B BInstance;
public void InvokeA()
{
BInstance = new B();
Console.WriteLine(BInstance.GetAInstance());
}
}
and then you will create your C instance in Main:
static void Main(string[] args)
{
C cInstance = new C();
cInstance.InvokeA();
}
In order to accomplish your goal, you need to expose B.MyInstance as a property of the B class, just like you exposed A.MyProperty as a property of the A class.
Edit: Per the comments of others regarding use of the static keyword, here's what you might want your code to look like:
public class A
{
public int MyProperty { get; set; }
}
public static class B
{
static B()
{
MyInstance = new A();
MyInstance.MyProperty = 10;
}
public static A MyInstance { get; set; }
}
public class C
{
// not sure what your intention is here
public C()
{
System.Console.WriteLine(B.MyInstance.MyProperty.ToString()); // "10\n"
}
}
Yes. You can inherits classes from A to B something like this:
public class A
{
public int MyProperty {get; set;}
}
public class B : A
{
public B()
: A()
{
MyProperty = 1;
}
}
Now you can do:
(new B()).MyProperty
Or use Singleton approach to resolve:
public class B
{
private static _a;
public class A
{
public int MyProperty {get; set;}
}
public static A AA {
if (_a == null) {
_a = new A();
}
return _a;
}
}
This implmentation will return
B.A.MyProperty.ToString();
The problem I'm actually working on is related to mappers in ASP.NET MVC but that's way too complex to post on SO, so I've simplified the issue I'm having below. I'll post my code first as it's easier to explain what I'm trying to achieve after the code.
Supporting Code
public abstract class BaseFoo
{
public int CommonProperty { get; set; }
}
public class Foo1 : BaseFoo
{
public int SomeProperty { get; set; }
}
public class Foo2 : BaseFoo
{
public int AnotherProperty { get; set; }
}
public interface IMyInterface<T>
{
void SomeMethod(T t);
}
public abstract class BaseClass<T> : IMyInterface<T>
where T : BaseFoo
{
public virtual void SomeMethod(T t)
{
t.CommonProperty = 1;
}
}
public class ConcreteClass1 : BaseClass<Foo1>
{
public override void SomeMethod(Foo1 t)
{
t.SomeProperty = 57;
base.SomeMethod(t);
}
}
public class ConcreteClass2 : BaseClass<Foo2>
{
public override void SomeMethod(Foo2 t)
{
t.AnotherProperty = 123;
base.SomeMethod(t);
}
}
public static class ConcreteClassFactory
{
public enum ConcreteClassType
{
ConcreteClass1,
ConcreteClass2
}
public static dynamic CreateClass(ConcreteClassType type)
{
dynamic toReturn = null;
switch (type)
{
case ConcreteClassType.ConcreteClass1:
toReturn = new ConcreteClass1();
break;
case ConcreteClassType.ConcreteClass2:
toReturn = new ConcreteClass2();
break;
default:
break;
}
return toReturn;
}
}
What I want to do is dynamically create different ConcreteClasss and call SomeMethod on that created object, basically I want to pass around my ConcreteClasss as BaseClass, much like you can pass around Foos as BaseFoo. I've gotten it to work with the following code:
class Program
{
static void Main(string[] args)
{
BaseFoo foo = new Foo1();
dynamic bar = ConcreteClassFactory.CreateClass(ConcreteClassFactory.ConcreteClassType.ConcreteClass1);
bar.SomeMethod(foo as dynamic);
}
}
However, this seems very kludgy to cast to a dynamic (also I don't fully understand why removing as dynamic throws a RuntimeBinderException, if someone can explain what's going on that would be appreciated). Is there a better way to achieve what I'm trying to do here?
With the constraints you have what I would do would be to cast and throw errors from inside the overridden SomeMethod.
public abstract class BaseClass : IMyInterface<BaseFoo>
{
public virtual void SomeMethod(BaseFoo t)
{
t.CommonProperty = 1;
}
}
public class ConcreteClass1 : BaseClass
{
public override void SomeMethod(BaseFoo t)
{
if(t == null)
throw new ArgumentNullException(nameof(t));
var foo1 = t as Foo1;
if(foo1 == null)
throw new NotSupportedException($"{nameof(ConcreteClass1)} does not support types other than {nameof(Foo1)}");
foo1.SomeProperty = 57;
base.SomeMethod(foo1);
}
}
public class ConcreteClass2 : BaseClass
{
public override void SomeMethod(BaseFoo t)
{
if (t == null)
throw new ArgumentNullException(nameof(t));
var foo2 = t as Foo2;
if (foo2 == null)
throw new NotSupportedException($"{nameof(ConcreteClass2)} does not support types other than {nameof(Foo2)}");
foo2.AnotherProperty = 123;
base.SomeMethod(foo2);
}
}
public static class ConcreteClassFactory
{
public enum ConcreteClassType
{
ConcreteClass1,
ConcreteClass2
}
public static BaseClass CreateClass(ConcreteClassType type)
{
BaseClass toReturn = null;
switch (type)
{
case ConcreteClassType.ConcreteClass1:
toReturn = new ConcreteClass1();
break;
case ConcreteClassType.ConcreteClass2:
toReturn = new ConcreteClass2();
break;
default:
break;
}
return toReturn;
}
}
Used like
class Program
{
static void Main(string[] args)
{
BaseFoo foo = new Foo1();
var bar = ConcreteClassFactory.CreateClass(ConcreteClassFactory.ConcreteClassType.ConcreteClass1);
bar.SomeMethod(foo);
}
}
Due to the polymorphism property of the classes, below example will print AB twice, which is expected.
In my case, I really want it to print A then AB.
I decided to change the Get() method in B from overrides to new.
This solves my problem, but they informed me of bad practise, so I'm looking for an alternative...
The one thing that comes to mind is to instantiate a new A in B.Do(), which I think is also bad practise...
//ORIGINAL
class Program
{
static void Main(string[] args)
{
var b = new B();
b.Do();
}
}
public class A
{
public virtual void Do()
{
var get = Get();
Console.WriteLine(get);
}
public virtual string Get()
{
return "A";
}
}
public class B : A
{
public override void Do()
{
base.Do();
var get = Get();
Console.WriteLine(get);
}
public override string Get()
{
return base.Get() + "B";
}
}
//UPDATED, USING NEW
class Program
{
static void Main(string[] args)
{
var b = new B();
b.Do();
Console.ReadLine();
}
}
public class A
{
public virtual void Do()
{
var get = Get();
Console.WriteLine(get);
}
public virtual string Get()
{
return "A";
}
}
public class B : A
{
public override void Do()
{
base.Do();
var get = Get();
Console.WriteLine(get);
}
public new string Get()
{
return base.Get() + "B";
}
}
Instead of calling the base version of Do in B.Do, you can change the implementation as follows:
public class B : A
{
public override void Do()
{
// Call the base version of Get explicitly
var getBase = base.Get();
Console.WriteLine(getBase);
// Call the current implementation of Get
var get = Get();
Console.WriteLine(get);
}
public override string Get()
{
return "B";
}
}
This will technically solve your problem, but is not a really clean solution from an OOP point of view. I suggest to think a bit about whether you need to be able to override Get independently. Maybe changing the signatures of your methods so that Get always returns a list of strings that should be printed is also a good solution (I've renamed Get to GetLines to reflect the changed purpose of the method):
public class A
{
public virtual void Do()
{
var lines = GetLines();
foreach(var line in lines)
Console.WriteLine(line);
}
public virtual IEnumerable<string> GetLines()
{
return new string[] { "A" };
}
}
public class B : A
{
public override IEnumerable<string> GetLines()
{
var lst = new List<string>(base.GetLines());
lst.Add("B");
return lst;
}
}
Here is the corrected code:
class Program
{
static void Main(string[] args)
{
var b = new B();
b.Do();
}
}
public class A
{
public virtual void Do()
{
Console.WriteLine(Get());
}
public virtual string Get()
{
return "A";
}
}
public class B : A
{
public override void Do()
{
Console.WriteLine(base.Get());
base.Do();
}
public override string Get()
{
return base.Get() + "B";
}
}