I have a class with 2 constructors:
public class Lens
{
public Lens(string parameter1)
{
//blabla
}
public Lens(string parameter1, string parameter2)
{
// want to call constructor with 1 param here..
}
}
I want to call the first constructor from the 2nd one. Is this possible in C#?
Append :this(required params) at the end of the constructor to do 'constructor chaining'
public Test( bool a, int b, string c )
: this( a, b )
{
this.m_C = c;
}
public Test( bool a, int b, float d )
: this( a, b )
{
this.m_D = d;
}
private Test( bool a, int b )
{
this.m_A = a;
this.m_B = b;
}
Source Courtesy of csharp411.com
Yes, you'd use the following
public class Lens
{
public Lens(string parameter1)
{
//blabla
}
public Lens(string parameter1, string parameter2) : this(parameter1)
{
}
}
The order of constructor evaluation must also be taken into consideration when chaining constructors:
To borrow from Gishu's answer, a bit (to keep code somewhat similar):
public Test(bool a, int b, string c)
: this(a, b)
{
this.C = c;
}
private Test(bool a, int b)
{
this.A = a;
this.B = b;
}
If we change the evalution performed in the private constructor, slightly, we will see why constructor ordering is important:
private Test(bool a, int b)
{
// ... remember that this is called by the public constructor
// with `this(...`
if (hasValue(this.C))
{
// ...
}
this.A = a;
this.B = b;
}
Above, I have added a bogus function call that determines whether property C has a value. At first glance, it might seem that C would have a value -- it is set in the calling constructor; however, it is important to remember that constructors are functions.
this(a, b) is called - and must "return" - before the public constructor's body is performed. Stated differently, the last constructor called is the first constructor evaluated. In this case, private is evaluated before public (just to use the visibility as the identifier).
Related
I have a BaseClass with a constructor and some parameters and then I want to do a strategy where depending of an enum, it creates one derived class or another (of that BaseClass), but with the same parameters. Is there any way to refactor this ? Thanks !
public enum GameMode
{
ModeA,
ModeB
}
public abstract class BaseClass
{
public BaseClass(int a, string b, char c)
{
}
}
public class FirstGameMode : BaseClass
{
public FirstGameMode(int a, string b, char c) : base(a, b, c)
{
}
}
public class SecondGameMode: BaseClass
{
public SecondGameMode(int a, string b, char c) : base(a, b, c)
{
}
}
public class TestingPurpose
{
private GameMode _gameMode;
private BaseClass _baseClass;
public void Init()
{
if (_gameMode == GameMode.ModeA)
{
// They use the same variables !
_baseClass = new FirstGameMode(5, "Hello", 'c');
}
else
{
// They use the same variables !
_baseClass = new SecondGameMode(5, "Hello", 'c');
}
}
}
I tried with some reflection but still I couldn't do it.
I would like to have something like
public void Init()
{
BaseMatchMode type;
if (_gameMode == GameMode.ModeA)
{
type = typeof(FirstGameMode);
}
else
{
type = typeof(SecondGameMode);
}
_baseClass = new type(5, "Hello", 'c');
}
You could use a factory delegate method;
Func<int a, string b, char c, BaseClass> factory;
if (_gameMode == GameMode.ModeA)
{
factory= (a, b, c) => new FirstGameMode(a, b, c);
}
else
{
factory= (a, b, c) => new SecondGameMode(a, b, c);
}
_baseClass = factory(5, "Hello", 'c');
For such an simple example it would probably be easier to just skip the factory method and create your objects directly. But this technique is sometimes useful if you want to add some abstraction between components.
You could also create a factory class instead of just using a delegate. There are also dependency injection (DI) / Inversion of Control (IoC) frameworks that are intended to solve the problem of specifying what implementation of interfaces/base classes other components should use.
I have the following code:
struct test {
public int a;
public int b;
public test(int a) {
this(a, null);
}
public test(int a, int b) {
this.a = a;
this.b = b;
}
}
Where I would like to have two different constructors for the test struct, one where I only need to pass in a and another where I can pass in both a and b.
This code does not work, as it fails with a few errors:
For the public test(int a) { line:
Field 'test.a' must be fully assigned before control is returned to the caller
Field 'test.b' must be fully assigned before control is returned to the caller
And for the this(a, null); line:
Method name expected.
The 'this' object cannot be used before all of its fields have been assigned
struct test {
public int a;
public int b;
public test(int a) : this(a, 0) { }
public test(int a, int b = 0) {
this.a = a;
this.b = b;
}
}
You can't assign null to int. Also your variable names are ambiguous. You can use an optional parameter to achieve what you're looking for. Or chaining constructors.
Try this
struct Test
{
public readonly int a;
public readonly int b;
public Test(int a) : this()
{
this.a = a;
this.b = 0;
}
public Test(int a, int b) : this()
{
this.a = a;
this.b = b;
}
}
an alternative is to use an optional parameter with one constructor
public Test(int a, int b = 0) : this()
{
this.a = a;
this.b = b;
}
or instead of 0 to use default(int), or just default
public Test(int a) : this()
{
this.a = a;
this.b = default(int);
}
The : this() call is added by Microsoft when the create constructor tool is used so I have added it here. I think it is just to remind the reader of the code that space in the stack is allocated first and then the fields are assigned.
I also added the readonly keyword because mutable structures are evil and to emphasize the requirements the fields need to be all defined before the constructor ends.
Or you can always use one constructor from another constructor
public Test(int a) : this(a, 0)
{ }
I am writing a class that should ideally have multiple methods of the same signature. Is there a way to force the class to check its methods if all of them follow the same signature?
It would be ideal if the check could be done at compile-time/during build
If you assume the signature to be int <methodName>(string, int, char)
public class Conditions {
// no error
int MethodA(string a, int b, char c)
{
return 0;
}
// no error
int MethodB(string a, int b, char c)
{
return 1;
}
// should throw error because return type does not match signature
string MethodC(string a, int b, char c)
{
return "Should throw an error for this function";
}
}
}
It's sort of cheating, but if you require the developer to register their methods, you can force a compile time error by requiring the method to match a delegate.
This is essentially how event handlers and callbacks work.
namespace Framework
{
public delegate int MyApiSignature(int a, string b, char c);
public class Core
{
static public void RegisterMethod(MyApiSignature method)
{
//Doesn't even have to actually do anything
}
}
}
namespace Custom
{
using Framework;
class Foo
{
public Foo()
{
Core.RegisterMethod(MethodA); //Works
Core.RegisterMethod(MethodB); //Compile-time error
}
public int MethodA(int a, string b, char c)
{
return 0;
}
public int MethodB(int a, string b, byte c)
{
return 0;
}
}
}
You could make a unit test:
[TestMethod]
public void Conditions_MethodsHaveCorrectSignature()
{
var whitelist = new List<string> { "Finalize", "MemberwiseClone" };
var t = typeof(Conditions);
var m = t.GetMethods(BindingFlags.NonPublic | BindingFlags.Instance);
foreach (var item in m.Where(x => !whitelist.Contains(x.Name)))
{
Assert.AreEqual(typeof(int), item.ReturnType);
CollectionAssert.AreEquivalent(new List<Type> { typeof(string), typeof(int), typeof(char) },
item.GetParameters().Select(x => x.ParameterType).ToList());
}
}
Not directly. You could write an analyzer for it using Roslyn, or you could write a unit test that checks the signatures via reflection.
I have a client application that uses classes (and enums) from an external dll which is loaded at runtime and reflected. I know what methods I am expecting to find in the dll and what I am expecting its enums to be called.
I would like to create a delegate that I can use in the client application and which is created from the reflected method at runtime. This approach works when the delegate just has "standard" types, but how can I get this to work if the dll method takes an enum? I can't declare the enum in the delegate as an object since it's a value type, trying the Enum or int does not seem to work either. Is there a way around this? Any help gratefully received!
// e.g. external code
namespace test2
{
public static class test2
{
public static int calc(int a, int b, testEnum c)
{
if (c == testEnum.add) return a + b;
else return a - b;
}
public static int add(int a, int b)
{
return a + b;
}
}
public enum testEnum
{
add, subtract
}
}
// my client code
namespace test1
{
public class TestClient
{
private static Assembly _assembly;
public static void SetUp()
{
const string externalDll = ".../test2.dll";
Assembly assembly = Assembly.LoadFrom(externalDll);
AppDomain.CurrentDomain.Load(assembly.GetName());
_assembly = assembly;
}
private delegate int _add(int a, int b);
private _add add;
private delegate int _calc(int a, int b, ??? c); // nothing works here
private _calc calc;
public void Run()
{
SetUp();
add = GetExpectedFunction<_add>("add");
int three = add(1, 2); // OK
calc = GetExpectedFunction<_calc>("calc"); // not OK
// intended usage
var reflectedEnum = ReflectMe("testEnum", "add");
int threeAgain = calc(1, 2, reflectedEnum);
}
public static T GetExpectedFunction<T>(string functionName) where T : class
{
try
{
if (!typeof(T).IsSubclassOf(typeof(Delegate))) throw new ApplicationException("GetExpectedFunction must return a delegate!");
var foundMethod = _assembly.GetType("test2.test2").GetMethod(functionName, BindingFlags.Public | BindingFlags.Static);
return (T)(object)Delegate.CreateDelegate(typeof(T), foundMethod);
}
catch (Exception e)
{
// "Error binding to target method!"
}
}
}
}
You can bind a delegate with object type to a method taking an enum by creating, at runtime, a dynamic method call with LINQ Expression, and adding data conversions for parameters whose types don't match:
public static T GetExpectedFunction<T>(string functionName) where T : class {
try {
if (!typeof(T).IsSubclassOf(typeof(Delegate))) throw new ApplicationException("GetExpectedFunction must return a delegate!");
var foundMethod = Type.GetType("test2.test2").GetMethod(functionName, BindingFlags.Public | BindingFlags.Static);
var inv = typeof(T).GetMethod("Invoke");
var parameters = inv.GetParameters().Zip(foundMethod.GetParameters(), (a, b) => new {
PassedIn = a.ParameterType
, Reflected = b.ParameterType
, Parameter = Expression.Parameter(a.ParameterType)
}).ToList();
if (parameters.All(p => p.PassedIn == p.Reflected)) {
// Bind directly
return (T)(object)Delegate.CreateDelegate(typeof(T), foundMethod);
}
var call = Expression.Call(foundMethod, parameters.Select(
p => p.PassedIn==p.Reflected
? (Expression)p.Parameter
: Expression.Convert(p.Parameter, p.Reflected)
));
return (T) (object) Expression.Lambda(typeof(T), call, parameters.Select(p => p.Parameter)).Compile();
} catch (Exception e) {
// "Error binding to target method!"
return null;
}
}
This implementation pairs up types from the reflected and the delegate methods (see parameters variable), and creates ParameterExpression objects for types that come from the delegate. Then it checks if all parameter types match up (the parameters.All(...) part). This is an optimization for situations when conversions are unnecessary.
If at least one conversion is necessary, the code creates a method call that substitutes the original parameter expressions with conversion expressions where types do not match up, creates a lambda of the requested delegate type, compiles it, and returns it to the caller.
For your code this dynamic method would look like this:
int dynamic_method(int a, int b, object c) {
return test2.test2(a, b, (testEnum)c);
}
There is one solution, you have to create faked Enum (it will be better if you create the exact enum), then you will pass as integer like this:
private delegate int _add(int a, int b);
private _add add;
private delegate int _calc(int a, int b, FakedEnum c); // faked enum here
private _calc calc;
public enum FakedEnum
{
}
public void Run()
{
SetUp();
add = GetExpectedFunction<_add>("add");
int three = add(1, 2); // OK
calc = GetExpectedFunction<_calc>("calc"); // it will be ok
var result= calc(4, 6, (FakedEnum)0);
// intended usage
// var reflectedEnum = ReflectMe("testEnum", "add");
//int threeAgain = calc(1, 2, reflectedEnum);
}
Use dynamic keyword to declare your delegate parameter:
private delegate int _calc(int a, int b, dynamic c);
how can i transform a method (that performs a+b and returns the result) from add(a,b) to a.add(b)?
i read this somewhere and i can't remember what is the technique called...
does it depends on the language?
is this possible in javascript?
In .NET it is called extension methods.
public static NumberExtensions
{
public static int Add(this int a, int b)
{
return a + b;
}
}
UPDATE:
In javascript you could do this:
Number.prototype.add = function(b) {
return this + b;
};
var a = 1;
var b = 2;
var c = a.add(b);
On c# it is named extensions methods:
public static class IntExt
{
public static int Add(this int a, int b)
{
return a + b;
}
}
...
int c = a.Add(b);
say for example you want to do this on integers in C#. You need to define extension methods like this:
public static class IntExtMethods
{
public static int add(this int a, int b)
{
return a+b;
}
}
In C# you can use an Extension Method. In C++, you need to create a member which belongs to the A class which performs the add for you. C does not have objects, so what you're looking for is not possible in C.
If you want to create your own JavaScript class:
function Num(v) {
this.val = v;
}
Num.prototype = {
add: function (n) {
return new Num(this.val + n.val);
}
};
var a = new Num(1);
var b = new Num(2);
var c = a.add(b); // returns new Num(3);
Taking your question literally, I assume you mean transforming this
var add = function(a, b) {
return a + b;
}
to this:
a.add = function(b) {
return this + b;
}
This however only adds that method to a, not to any other object with the same constructor. See Darin Dimitrov's answer for an example of that. Extending the native Number constructor's prototype is not something many would recommend though...