the easiest way to describe my problem is with example code. I know that this won't compile, but i need a similar option
abstract class Foo
{
protected abstract static ElementName {get;}
}
class Bar : Foo
{
protected static override ElementName
{
get
{
return "bar";
}
}
}
class Baz<T> where T : Foo
{
public string ElementName
{
get
{
return T.ElementName;
}
}
}
Grettings
This cannot be done in the way you want, but you can achieve something similar using reflection. Here is an example offering two possible solutions to your problem (updated):
abstract class Foo
{
protected abstract string _ElementName { get; }
public static string GetElementName<T>() where T : Foo, new()
{
return typeof(T).GetProperty("_ElementName", BindingFlags.Instance | BindingFlags.NonPublic)?
.GetValue(new T()) as string;
}
public static string GetStaticElementName<T>() where T : Foo, new()
{
return typeof(T).GetProperty("ElementName", BindingFlags.Static | BindingFlags.NonPublic)?
.GetValue(null) as string;
}
}
class Bar : Foo
{
protected static string ElementName
{
get
{
return "StaticBar";
}
}
protected override string _ElementName
{
get
{
return "Bar";
}
}
}
class FooBar : Bar
{
protected static string ElementName
{
get
{
return "StaticFooBar";
}
}
protected override string _ElementName
{
get
{
return "FooBar";
}
}
}
class Baz<T> where T : Foo, new()
{
public string ElementName
{
get
{
return Foo.GetElementName<T>();
}
}
public string StaticElementName
{
get
{
return Foo.GetStaticElementName<T>();
}
}
}
...
Console.WriteLine(new Baz<Bar>().ElementName); // Bar
Console.WriteLine(new Baz<FooBar>().ElementName); // FooBar
Console.WriteLine(new Baz<Bar>().StaticElementName); // StaticBar
Console.WriteLine(new Baz<FooBar>().StaticElementName); // StaticFooBar
Related
I had an interview today and it had the following code with 2 questions. Could someone please help me answer the two questions below on the below code snippet. (Some minor syntax error if seen please ignore as I tried to remember it from mind)....
Question 1 - Change the name to "NameChange".
Question 2 - Override the abstract method in derived class.
public abstract Class A
{
public string Name { get { return GetName(); } };
public virtual string GetName()
{
return this.Name.ToString();
}
protected abstract void SomeMethod();
}
public class B : A
{
//Change name to "NameChange"
//override the abstract method here
}
I'm not sure whether there's a hidden question or those are as simple as this. For the first question, there're two answers (method 1 and 2):
public class B : A
{
//Method 1
public new string Name { get { return "NameChange"; } }
//Method 2
public override string GetName()
{
return "NameChange"; // return whatever you want
}
protected override void SomeMethod() // override abstract method
{
// do something
}
}
In Method 1, you are effectively hiding the implementation of Name of the base class. In Method 2, you are overriding the implementation of GetName.
This should answer both questions:
public class B : A
{
//override Method
public override string GetName()
{
return "NameChange";
}
// override abstract Method
protected override void SomeMethod()
{
//code here...
}
}
I'm not sure if i get it right, but is that what you need?
public class B : A
{
public string NameChange => base.GetName();
protected override void SomeMethod()
{
throw new NotImplementedException();
}
}
Or maybe this one?
public class B : A
{
public override string GetName()
{
return "NameChange";
}
protected override void SomeMethod()
{
throw new NotImplementedException();
}
}
I am not sure what is the point here but if they insist on changing the value of property name, new constraint can be the trick here.
You can find the implementation following;
public abstract class A
{
public string Name { get { return GetName(); } }
public virtual string GetName()
{
return this.Name.ToString();
}
protected abstract void SomeMethod();
}
public class B : A
{
public B() : base()
{
SomeMethod();
}
public new string Name { get; set; }
public override string GetName()
{
return Name;
}
protected override void SomeMethod()
{
this.Name = "Ayberk";
}
}
public class Program
{
public static void Main(string[] args)
{
//Your code goes here
B b = new B();
Console.WriteLine(b.GetName());
}
}
You can try it here
Having trouble figuring out how to manage contextual binding in the scenario where two classes have the same underlying interface dependency, but each class ctor's parameter is named differently. Pseudo code below to demonstrate my situation:
interface IThing { }
public class Thing1 : IThing { public Thing1(string fileCode) { } }
public class Thing2 : IThing { public Thing2(string fileCode) { } }
interface IThingFactory { IThing CreateThing(string fileCode); }
interface IDependentThing { }
public class A : IDependentThing { public A(string fileCode, IThingFactory thingFactory) { } }
public class B : IDependentThing { public B(string fileCd, IThingFactory thingFactory) { } } //How to handle binding for this dependent?
interface IDependentThingFactory { IDependentThing CreateDependentThing(string fileCode); }
//...
public override void Load()
{
Bind<IThing>().ToMethod(ctx =>
{
var fileCode = ctx.Parameters.First(p => p.Name == "fileCode").GetValue(ctx, null) as string;
IThing thing = null;
if (fileCode == "FileType1")
{
Bind<Thing1>().ToSelf().WithConstructorArgument("fileCode", fileCode);
thing = Kernel.Get<Thing1>();
}
else if (fileCode == "FileType2")
{
Bind<Thing2>().ToSelf().WithConstructorArgument("fileCode", fileCode);
thing = Kernel.Get<Thing2>();
}
return thing;
});
Bind<IThingFactory>().ToFactory();
Bind<IDependentThingFactory>().ToFactory();
}
//Later...
using (TextReader tr = new StreamReader(path))
{
string firstLine = tr.ReadLine();
if (firstLine.Substring(838, 1) == ".")
{
fileCode = "FileType1";
}
else if (firstLine.Substring(883, 1) == ".")
{
fileCode = "FileType2";
}
//won't work for creating B
Kernel.Get<IDependentThing>(new ConstructorArgument("fileCode", fileCode));
//or maybe...
//seems to eliminate my problem by allowing me to handle variations
//in parameter names from within A and B's ctors, but looks like it
//requires injecting factories along the chain (see A & B ctor arguments).
dependentThingFactory.CreateDependentThing(fileCode)
};
fileCode is computed based off of some analysis of local files. Once the type of file is determined, I want Ninject to hand back the appropriate object for processing that file
How would I handle the binding for B since the existing binding I defined requires a constructor parameter with a different name? Is there a better way to do this in general?
I guess I could just use p.Name == "fileCode" || p.Name == "fileCd", but I can't shake the feeling that I'm doing something wrong (feels messy). Also, I'm not thrilled about pulling parameters by name, and I've thought about maybe creating a custom type that would give Ninject something more concrete to match against versus a string parameter. From where I'm standing it looks like I either just manage the multiple parameter names situation, or switch to custom types as my parameters instead of strings.
Making parameter injection more refactor safe and making them available for the whole resolution context
Instead of "named parameters" you can use a "type matching" or "typed" parameter. The factories IInstanceProvider can be exchanged for another one which does so:
kernel.Bind<IThingFactory>()
.ToFactory(() => new TypeMatchingArgumentInheritanceInstanceProvider());
Note:
this IInstanceProvider will also make the argument available further "downstream" (it "inherits" the parameter)
a string is very verbose so you may want to choose to wrap it in another type, like class ConnectionInfo.
Contextual Binding combined with parameter injection
So let's say we create our own FileType type to be more verbose than just using string:
public class FileCode
{
public FileCode(string value)
{
Value = value;
}
public string Value { get; private set; }
}
(maybe you want to replace that with an enum?)
Since your requirements are more complex we're going to have to change things up a little bit.
We're going to create our own IConstructorArgument to easily be able to match it for When-contextual bindings and also inject it's value based on type-matching (as above):
internal class FileCodeParameter : IConstructorArgument
{
private readonly FileCode fileCode;
public FileCodeParameter(FileCode fileCode)
{
this.fileCode = fileCode;
}
public string Name { get { return "File Code Parameter"; } }
public bool ShouldInherit { get { return true; } }
public FileCode FileCode { get { return this.fileCode; } }
public bool Equals(IParameter other)
{
var otherFileCodeParameter = other as FileCodeParameter;
if (otherFileCodeParameter == null)
{
return false;
}
return otherFileCodeParameter.fileCode == this.fileCode;
}
public object GetValue(IContext context, ITarget target)
{
return this.fileCode;
}
public bool AppliesToTarget(IContext context, ITarget target)
{
return target.Type == typeof(FileCode);
}
}
Now let me create some sample codes so we can later verify that it works:
public interface IThing
{
FileCode FileCode { get; }
}
public abstract class Thing : IThing
{
protected Thing(FileCode fileCode)
{
FileCode = fileCode;
}
public FileCode FileCode { get; private set; }
}
public class ThingFoo : Thing
{
public ThingFoo(FileCode fileCode) : base(fileCode) { }
}
public class ThingBar : Thing
{
public ThingBar(FileCode fileCode) : base(fileCode) { }
}
public interface IOtherThing
{
FileCode FileCode { get; }
}
public abstract class OtherThing : IOtherThing
{
protected OtherThing(FileCode fileCode)
{
FileCode = fileCode;
}
public FileCode FileCode { get; private set; }
}
public class OtherThingFoo : OtherThing
{
public OtherThingFoo(FileCode fileCode) : base(fileCode) { }
}
public class OtherThingBar : OtherThing
{
public OtherThingBar(FileCode fileCode) : base(fileCode) { }
}
public class OtherThingWrapper
{
public OtherThingWrapper(IOtherThing otherThing)
{
OtherThing = otherThing;
}
public IOtherThing OtherThing { get; private set; }
}
public class FileProcessor
{
public FileProcessor(IThing thing, OtherThingWrapper otherThingWrapper)
{
Thing = thing;
OtherThingWrapper = otherThingWrapper;
}
public IThing Thing { get; private set; }
public OtherThingWrapper OtherThingWrapper { get; private set; }
}
What's missing? The factory. We could use the ToFactory binding with custom IInstanceProvider but unless we're going to create lots of factories with FileCodeParameters i don't think it makes sense, so let's keep it simple:
public interface IFileProcessorFactory
{
FileProcessor Create(FileCode fileCode);
}
internal class FileProcessorFactory : IFileProcessorFactory
{
private readonly IResolutionRoot resolutionRoot;
public FileProcessorFactory(IResolutionRoot resolutionRoot)
{
this.resolutionRoot = resolutionRoot;
}
public FileProcessor Create(FileCode fileCode)
{
return this.resolutionRoot.Get<FileProcessor>(new FileCodeParameter(fileCode));
}
}
Now let's have it all come together:
public class Test
{
[Fact]
public void FactMethodName()
{
var fooFileCode = new FileCode("foo");
var barFileCode = new FileCode("bar");
var kernel = new StandardKernel();
kernel
.Bind<IFileProcessorFactory>()
.To<FileProcessorFactory>();
kernel
.Bind<IThing>()
.To<ThingFoo>()
.WhenFileCode(fooFileCode);
kernel
.Bind<IThing>()
.To<ThingBar>()
.WhenFileCode(barFileCode);
kernel
.Bind<IOtherThing>()
.To<OtherThingFoo>()
.WhenFileCode(fooFileCode);
kernel
.Bind<IOtherThing>()
.To<OtherThingBar>()
.WhenFileCode(barFileCode);
var fileProcessor = kernel.Get<IFileProcessorFactory>().Create(barFileCode);
fileProcessor.Thing.Should().BeOfType<ThingBar>();
fileProcessor.Thing.FileCode.Should().Be(barFileCode);
fileProcessor.OtherThingWrapper.OtherThing.Should().BeOfType<OtherThingBar>();
fileProcessor.OtherThingWrapper.OtherThing.FileCode.Should().Be(barFileCode);
}
}
public static class BindingExtensionsForFileCodes
{
public static IBindingInNamedWithOrOnSyntax<T> WhenFileCode<T>(
this IBindingWhenSyntax<T> syntax,
FileCode fileCode)
{
return syntax.When(req => req
.Parameters
.OfType<FileCodeParameter>()
.Single()
.FileCode.Value == fileCode.Value);
}
}
That's it!
- the FileCode is both being injected and being used for selection of implementation
- as the parameter is "inherited", it also works deeper down the object tree.
Below, just for reference, all of the code for easier copy & paste:
using FluentAssertions;
using Ninject;
using Ninject.Activation;
using Ninject.Parameters;
using Ninject.Planning.Targets;
using Ninject.Syntax;
using System.Linq;
using Xunit;
namespace NinjectTest.ParameterContextual
{
public class FileCode
{
public FileCode(string value)
{
Value = value;
}
public string Value { get; private set; }
}
public interface IThing
{
FileCode FileCode { get; }
}
public abstract class Thing : IThing
{
protected Thing(FileCode fileCode)
{
FileCode = fileCode;
}
public FileCode FileCode { get; private set; }
}
public class ThingFoo : Thing
{
public ThingFoo(FileCode fileCode) : base(fileCode) { }
}
public class ThingBar : Thing
{
public ThingBar(FileCode fileCode) : base(fileCode) { }
}
public interface IOtherThing
{
FileCode FileCode { get; }
}
public abstract class OtherThing : IOtherThing
{
protected OtherThing(FileCode fileCode)
{
FileCode = fileCode;
}
public FileCode FileCode { get; private set; }
}
public class OtherThingFoo : OtherThing
{
public OtherThingFoo(FileCode fileCode) : base(fileCode) { }
}
public class OtherThingBar : OtherThing
{
public OtherThingBar(FileCode fileCode) : base(fileCode) { }
}
public class OtherThingWrapper
{
public OtherThingWrapper(IOtherThing otherThing)
{
OtherThing = otherThing;
}
public IOtherThing OtherThing { get; private set; }
}
public class FileProcessor
{
public FileProcessor(IThing thing, OtherThingWrapper otherThingWrapper)
{
Thing = thing;
OtherThingWrapper = otherThingWrapper;
}
public IThing Thing { get; private set; }
public OtherThingWrapper OtherThingWrapper { get; private set; }
}
public interface IFileProcessorFactory
{
FileProcessor Create(FileCode fileCode);
}
internal class FileProcessorFactory : IFileProcessorFactory
{
private readonly IResolutionRoot resolutionRoot;
public FileProcessorFactory(IResolutionRoot resolutionRoot)
{
this.resolutionRoot = resolutionRoot;
}
public FileProcessor Create(FileCode fileCode)
{
return this.resolutionRoot.Get<FileProcessor>(new FileCodeParameter(fileCode));
}
}
public class Test
{
[Fact]
public void FactMethodName()
{
var fooFileCode = new FileCode("foo");
var barFileCode = new FileCode("bar");
var kernel = new StandardKernel();
kernel
.Bind<IFileProcessorFactory>()
.To<FileProcessorFactory>();
kernel
.Bind<IThing>()
.To<ThingFoo>()
.WhenFileCode(fooFileCode);
kernel
.Bind<IThing>()
.To<ThingBar>()
.WhenFileCode(barFileCode);
kernel
.Bind<IOtherThing>()
.To<OtherThingFoo>()
.WhenFileCode(fooFileCode);
kernel
.Bind<IOtherThing>()
.To<OtherThingBar>()
.WhenFileCode(barFileCode);
var fileProcessor = kernel.Get<IFileProcessorFactory>().Create(barFileCode);
fileProcessor.Thing.Should().BeOfType<ThingBar>();
fileProcessor.Thing.FileCode.Should().Be(barFileCode);
fileProcessor.OtherThingWrapper.OtherThing.Should().BeOfType<OtherThingBar>();
fileProcessor.OtherThingWrapper.OtherThing.FileCode.Should().Be(barFileCode);
}
}
internal class FileCodeParameter : IConstructorArgument
{
private readonly FileCode fileCode;
public FileCodeParameter(FileCode fileCode)
{
this.fileCode = fileCode;
}
public string Name { get { return "File Code Parameter"; } }
public bool ShouldInherit { get { return true; } }
public FileCode FileCode { get { return this.fileCode; } }
public bool Equals(IParameter other)
{
var otherFileCodeParameter = other as FileCodeParameter;
if (otherFileCodeParameter == null)
{
return false;
}
return otherFileCodeParameter.fileCode == this.fileCode;
}
public object GetValue(IContext context, ITarget target)
{
return this.fileCode;
}
public bool AppliesToTarget(IContext context, ITarget target)
{
return target.Type == typeof(FileCode);
}
}
public static class BindingExtensionsForFileCodes
{
public static IBindingInNamedWithOrOnSyntax<T> WhenFileCode<T>(
this IBindingWhenSyntax<T> syntax,
FileCode fileCode)
{
return syntax.When(req => req
.Parameters
.OfType<FileCodeParameter>()
.Single()
.FileCode.Value == fileCode.Value);
}
}
}
I am playing with data builder pattern, and I am failing to understand some behaviours.
I wrote this simplified version of what I am trying to achieve below
public abstract class DataBuilderParent
{
private MyParent myParent;
protected void SetDataBuilder(MyParent myParent)
{
this.myParent = myParent;
}
public DataBuilderParent WithId(int id)
{
myParent.Id = id;
return this;
}
}
public class DataBuilderChild : DataBuilderParent
{
private readonly MyChild myChild = new MyChild();
public DataBuilderChild()
{
base.SetDataBuilder(myChild);
}
public DataBuilderChild WithDescription(string description)
{
myChild.Description = description;
return this;
}
private MyChild Build()
{
return myChild;
}
public static implicit operator MyChild(DataBuilderChild dataBuilder)
{
return dataBuilder.Build();
}
}
public class MyParent
{
public int Id { get; set; }
}
public class MyChild : MyParent
{
public string Description { get; set; }
}
Usage of the code above
internal class Program
{
private static void Main(string[] args)
{
MyChild child = new DataBuilderChild().WithDescription("");
}
}
Now it will create the child for me, also the intellisense shows up that I can do this new DataBuilderChild().WithId(1).WithDescription("");
however when I try to build it fails. I do not understand why I am not able to access this method ? it is public and its visible by the intellisense.
Can anybody explain how to make it work please?
Thank you
EDIT
The error message is: Error 1 Cannot implicitly convert type 'TestProgram.Program.DataBuilderParent' to 'TestProgram.Program.MyChild' C:\Apps\TestProgram\Program.cs 44 29 TestProgram
I just refactored my answer from the comments.
public class MyParent
{
public int Id { get; set; }
}
public class MyChild : MyParent
{
public string Description { get; set; }
}
Make the WithDescription method in the DataBuilderParent class as abstract.
public abstract class DataBuilderParent
{
private MyParent myParent;
protected void SetDataBuilder(MyParent myParent)
{
this.myParent = myParent;
}
public DataBuilderParent WithId(int id)
{
myParent.Id = id;
return this;
}
public abstract DataBuilderParent WithDescription(string description);
private MyChild BuildAsChild()
{
return myParent as MyChild;
}
private MyParent Build()
{
return myParent;
}
public static implicit operator MyChild(DataBuilderParent dataBuilder)
{
return dataBuilder.BuildAsChild();
}
public static implicit operator MyParent(DataBuilderParent dataBuilder)
{
return dataBuilder.Build();
}
}
Then the DataBuilderChild
public class DataBuilderChild : DataBuilderParent
{
private readonly MyChild myChild = new MyChild();
public DataBuilderChild()
{
base.SetDataBuilder(myChild);
}
public override DataBuilderParent WithDescription(string description)
{
myChild.Description = description;
return this;
}
private MyChild Build()
{
return myChild;
}
public static implicit operator MyChild(DataBuilderChild dataBuilder)
{
return dataBuilder.Build();
}
}
You would then build like this
class Program
{
static void Main(string[] args)
{
var childBuilder = new DataBuilderChild().WithId(1).WithDescription("Child");
MyParent parent = childBuilder;
MyChild child = childBuilder;
Console.WriteLine(#"Parent With Id {0}", parent.Id);
Console.WriteLine(#"Child With Id {0} and Desciprtion - {1}", child.Id, child.Description);
Console.ReadKey();
}
}
The return value of WithId is DataBuilderParent, which does not define the method WithDescription; this is only defined in the derived class DataBuilderChild. Apparently you aim at having some Named Constructor idiom on a class hierarchy, which cannot be implemented this way.
Furthermore, DataBuilderParent does not define a cast operator, neither to MyChild nor to MyParent, whereas DataBuilderChild defeines a cast operator to MyChild, as stated in the error message.
Suppose class A as:
public class A
{
private string _str;
private int _int;
public A(string str)
{
this._str = str;
}
public A(int num)
{
this._int = num;
}
public int Num
{
get
{
return this._int;
}
}
public string Str
{
get
{
return this._str;
}
}
}
I want to hide Str property when i construct class A as
new A(2)
and want to hide Num property when i construct class A as
new A("car").
What should i do?
That isn't possible with a single class. An A is an A, and has the same properties - regardless of how it is constructed.
You could have 2 subclasses of abstract A, and a factory method...
public abstract class A
{
class A_Impl<T> : A
{
private T val;
public A_Impl(T val) { this.val = val; }
public T Value { get { return val; } }
}
public static A Create(int i) { return new A_Impl<int>(i); }
public static A Create(string str) { return new A_Impl<string>(str); }
}
But : the caller will not know about the value unless they cast it.
use generics
public class A<T>
{
private T _value;
public A(T value)
{
this._value= value;
}
public TValue
{
get
{
return this._value;
}
}
}
I got an abstract class :
abstract class ClassBase
{
public abstract string Test { get; }
}
I want to derive it and by the way add a set accesor
class ClassDerive : ClassBase
{
string _s;
public override string Test
{
get { return _s; }
set { _s = value; }
}
}
I can't do that because i may not override set
class ClassDerive2 : ClassBase
{
string _s;
public string Test
{
override get { return _s; }
set { _s = value; }
}
}
Syntax error
class ClassDerive3 : ClassBase
{
string _s;
public override string ClassBase.Test
{
get { return _s; }
}
public string Test
{
set { _s = value; }
}
}
Syntax error
Any Idea ???
thx
You cannot do exactly what you want to do but here is a workaround:
abstract class ClassBase
{
public abstract String Test { get; }
}
class ClassDerive : ClassBase
{
string _s;
public override string Test
{
get { return _s; }
}
public void SetTest(String test)
{
this._s = test;
}
}
This will make Test only settable in ClassDerived via the public SetTest method. I know this is not as clean as using the property's setter but it is about as good as it's going to get.
If at first you have defined a read-only property in a type, you can't later change it to a read/write property in a derived class. That's simply how .NET works, and can't be changed.
If, on the other hand, you define an interface with a read-only property, you can later implement that interface in a class with a writable property.
If you'd like to share what you are trying to achieve, perhaps we can come up with a design that works and can compile :)
Another way:
abstract class ClassBase
{
public abstract string Test { get; }
}
class ClassDerive : ClassBase
{
string _s;
protected void setTest(string s)
{
_s = s;
}
public override string Test
{
get { return _s; }
}
}
class ClassDerive2 : ClassDerive
{
public new string Test
{
get { return base.Test; }
set { setTest(value); }
}
}
class Program
{
static void Main(string[] args)
{
var cd2 = new ClassDerive2();
cd2.Test = "asdf";
Console.WriteLine(cd2.Test);
}
}
My first thought was also to implement it as an interface. If this fits in with your design, the following code will work:
public interface TestInterface
{
string TestProperty { get; }
}
public class TestClass : TestInterface
{
public string TestProperty
{
get { return "test"; }
set { string t = value; }
}
}
No you cant, sorry. It is by design, so it's the law.