Question

EDITED: Sorry, but this sample is working... simplifying my problem I've found the origin of the problem: the type of the const. In the sample is "int" and in my real code is "short". Then I must reformulate the question: Why I can't access to a short constant? I can't change the type of the const.


I want to compare a property of an object with a constant defined in a inner class. Sample:

public class A {
    public const int A_CONST = 1;
    public class B {
        public const int AB_CONST = 1;
    }
}

Now when I try to evaluate an expression containing A.B.AB_CONST in the ExpressionEvaluator of spring.net, I don't know how can I do it. I've tried a lot of things but it didn't work.

I've registered type A:

TypeRegistry.RegisterType(typeof(A))

And I can access to A_CONST

ExpressionEvaluator.GetValue(null, "A.A_CONST")

But with class A.B, it doesn't work. I've tried to register the type with an alias, using '.' and '+'. Accessing it through different ways too, but no way has worked for me.

TypeRegistry.RegisterType(typeof(A.B))
ExpressionEvaluator.GetValue(null, "A.B.AB_CONST")
ExpressionEvaluator.GetValue(null, "A+B.AB_CONST")


TypeRegistry.RegisterType("AB", typeof(A.B))
ExpressionEvaluator.GetValue(null, "AB.AB_CONST")

TypeRegistry.RegisterType("A.B", typeof(A.B))
ExpressionEvaluator.GetValue(null, "A.B.AB_CONST")
ExpressionEvaluator.GetValue(null, "A+B.AB_CONST")

Any idea?

Was it helpful?

Solution

After some attempts I've found the solution. I've replaced 'const' by 'static' and it finally works:

Class constants definition:

public class A {
    public static short A_CONST = 1;
    public class B {
        public static short AB_CONST = 1;
    }
}

Register types explicitly:

TypeRegistry.RegisterType(typeof(A));
TypeRegistry.RegisterType(typeof(A.B));

Access to constants dynamically through ExpressionEvaluator:

object result = ExpressionEvaluator.GetValue(null, "A.A_CONST");
Assert.AreEqual(A.A_CONST, result);

result = ExpressionEvaluator.GetValue(null, "A.B.AB_CONST");
Assert.AreEqual(A.B.AB_CONST, result);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top