Service/Object Locater
-
Hi, As a part of one of my project implementation I was keen on using a centralized object creator along with the abstract fatory pattern. As a part of it I have written the following class to solve the purpose of creating an object given an interface:
public class SimpleServiceLocater : ProviderBase, IServiceLocater
{
Dictionary<string, ConstructorInfo> typesDictionary = new Dictionary<string, ConstructorInfo>();//Utils private object\[\] defaultParamsArray = new object\[\] { }; private Type\[\] defaultTypesArray = new Type\[\] { }; //private object\[\] defaultParamsArray = null; //private Type\[\] defaultTypesArray = null; public override void Initialize(string name, NameValueCollection config) { base.Initialize(name, config); } public override string Name { get { return base.Name; } } #region IServiceLocater Members public virtual bool Register<IT, CT>() where CT : IT, new() { return this.Register<IT, CT>(""); } public virtual bool Register<IT, CT>(string Name) where CT : IT, new() { ConstructorInfo t = null; if (!typesDictionary.TryGetValue(typeof(IT).FullName + Name, out t)) { t = null; } if(t == null) { this.Unregister<IT>(Name); } typesDictionary.Add(typeof(IT).FullName + Name, typeof(CT).GetConstructor(defaultTypesArray)); return true; } public virtual IT Resolve<IT>() where IT : class { return this.Resolve<IT>(""); } public virtual IT Resolve<IT>(string Name) where IT : class { ConstructorInfo t = null; if (!typesDictionary.TryGetValue(typeof(IT).FullName + Name, out t)) { t = null; } if (t == null) { return default(IT); } else { //return Activator.CreateInstance(t) as IT; return t.Invoke(defaultParamsArray) as IT; } } public virtual bool Unregister<IT>() { return this.Unregister<IT>(""); } public virtual bool Unregister<IT>(string Name) { return typesDictionary.Remove(typeof(IT).FullName + Name); } #endregion #region IDisposable Members public void Dispose() { this.typesDictionary.Clear(); } #endregion
}
However I am not sure ab
-
Hi, As a part of one of my project implementation I was keen on using a centralized object creator along with the abstract fatory pattern. As a part of it I have written the following class to solve the purpose of creating an object given an interface:
public class SimpleServiceLocater : ProviderBase, IServiceLocater
{
Dictionary<string, ConstructorInfo> typesDictionary = new Dictionary<string, ConstructorInfo>();//Utils private object\[\] defaultParamsArray = new object\[\] { }; private Type\[\] defaultTypesArray = new Type\[\] { }; //private object\[\] defaultParamsArray = null; //private Type\[\] defaultTypesArray = null; public override void Initialize(string name, NameValueCollection config) { base.Initialize(name, config); } public override string Name { get { return base.Name; } } #region IServiceLocater Members public virtual bool Register<IT, CT>() where CT : IT, new() { return this.Register<IT, CT>(""); } public virtual bool Register<IT, CT>(string Name) where CT : IT, new() { ConstructorInfo t = null; if (!typesDictionary.TryGetValue(typeof(IT).FullName + Name, out t)) { t = null; } if(t == null) { this.Unregister<IT>(Name); } typesDictionary.Add(typeof(IT).FullName + Name, typeof(CT).GetConstructor(defaultTypesArray)); return true; } public virtual IT Resolve<IT>() where IT : class { return this.Resolve<IT>(""); } public virtual IT Resolve<IT>(string Name) where IT : class { ConstructorInfo t = null; if (!typesDictionary.TryGetValue(typeof(IT).FullName + Name, out t)) { t = null; } if (t == null) { return default(IT); } else { //return Activator.CreateInstance(t) as IT; return t.Invoke(defaultParamsArray) as IT; } } public virtual bool Unregister<IT>() { return this.Unregister<IT>(""); } public virtual bool Unregister<IT>(string Name) { return typesDictionary.Remove(typeof(IT).FullName + Name); } #endregion #region IDisposable Members public void Dispose() { this.typesDictionary.Clear(); } #endregion
}
However I am not sure ab
This is so basic.
Manish.
-
This is so basic.
Manish.
-
Agreed. I am interested in knowing the downside of using Activator.Createinstance vs Constructor.Invoke and also about do's n dont's while using them.
Regards, Chandra V
When I had that same question, this blog post by the amazing Haibo Luo answered it quite well. Activator.CreateInstance and beyond[^] I hope you enjoy it.
"we must lose precision to make significant statements about complex systems." -deKorvin on uncertainty
-
When I had that same question, this blog post by the amazing Haibo Luo answered it quite well. Activator.CreateInstance and beyond[^] I hope you enjoy it.
"we must lose precision to make significant statements about complex systems." -deKorvin on uncertainty