View Full Version : Initializing static variables
samokk
03-30-2005, 07:48 PM
Hi,
I'm using Spring.Net for a piece of software that uses a static field that needs to be initialized.
Is there a way for Spring.Net to initialize static fields ? I'm currently using a dirty hack to get the reference to the object (ctx ["object"], but there is no more inversion of control....
Thanks for your help
Sami Dalouche
Mark Pollack
03-31-2005, 04:46 PM
Hi Sami,
Hmm no, there isn't a way. Static methods are excluded from what TypeDescriptor.GetProperties considers a property. I checked and JavaBeans also conform to the same convention as does Spring.Java's behavior.
We could introduce sytnax like this
<property name="mystaticproperty" static=true>
<value>foobar</value>
</property>
which would give a hint for spring to use the "raw" reflection api to set either a static method or static field of the given name. The introduction of the static attribute would be be to avoid making expensive reflection calls in the normal case.
Can you introduce a public setter or is it "3rd party" code.
Cheers,
Mark
samokk
03-31-2005, 06:31 PM
Can you introduce a public setter or is it "3rd party" code.
Cheers,
Mark
Hi,
Yes, I can introduce anything in my code. static method, static property, anything.. Is there any way to get through by adding a static method ?
Mark Pollack
03-31-2005, 06:48 PM
Hi,
Unforunately no, TypeDescriptor.GetProperties doesn't recognize static properties (as far as I know), which is what is used. It would have to be an instance method which sets a static field - that maybe a little goofy it will work.
Mark
Aleks Seovic
04-01-2005, 07:18 AM
This brings back memory of me asking for addition of type configuration, not only object configuration.
Basically, static properties ae a good way to configure class, which would automatically cause all instances of the class to share the information. What I would like to see is <type> top-level element that would be static equivalent to <object> element.
We'll talk about it ;)
Aleks
Toranaga
02-27-2008, 08:39 AM
This brings back memory of me asking for addition of type configuration, not only object configuration.
Basically, static properties ae a good way to configure class, which would automatically cause all instances of the class to share the information. What I would like to see is <type> top-level element that would be static equivalent to <object> element.
We'll talk about it ;)
Aleks
This sounds like a very good idea. It can be used to initialize a class, and specify default state for it's objects. Objects can then also be constructed without using a name/id...
Thomas.Darimont
02-27-2008, 11:09 AM
Hello,
you could use spring expressions for this:
Have a look at:
http://forum.springframework.net/showpost.php?p=11127&postcount=3
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace De.Tutorials.Spring
{
public class Foo
{
private IList<string> list;
static string SomeStaticVariable = "FOO";
public Foo()
{
this.list = new List<string>();
}
public IList<string> List
{
get
{
return list;
}
}
public override string ToString()
{
StringBuilder stringBuilder = new StringBuilder("[");
for (int i = 0,count = list.Count; i < count; i++)
{
stringBuilder.Append(list[i]);
if (i < count-1)
{
stringBuilder.Append(",");
}
}
return stringBuilder.Append("] " + SomeStaticVariable).ToString();
}
}
}
the config file:
<?xml version="1.0" encoding="utf-8" ?>
<objects xmlns='http://www.springframework.net'>
<object name="foo" type="De.Tutorials.Spring.Foo" >
</object>
<object type="De.Tutorials.Spring.SpringExpressionBasedObjectPos tProcessor">
<property name="ObjectNameToPostProcessingExpressionMap">
<dictionary key-type="string" value-type="System.Collections.Generic.IList<Spring.Expression s.IExpression>">
<entry key="foo">
<list element-type="Spring.Expressions.IExpression,Spring.Core">
<value type="Spring.Expressions.IExpression,Spring.Core">
<![CDATA[
(
#this.List.Add('AAA');
#this.List.Add('BBB');
SomeStaticVariable = 'BUBU'
)
]]>
</value>
</list>
</entry>
</dictionary>
</property>
</object>
</objects>
output:
[AAA,BBB] BUBU
Best regards,
Thomas
vBulletin® v3.7.3, Copyright ©2000-2008, Jelsoft Enterprises Ltd.