J
John B
I have a situation where I need to control the creation of a class.
I need to be able to create it externally but only by classes that I define.
To accomplish this I thought of the following process:
Define a delegate that returns an instance of my class.
Pass this an instance of this delegate to any classes that I want to be
able to create instances of myclass.
i.e.
//Safe class
using System;
using System.Collections.Generic;
using System.Text;
namespace DelegateCreation
{
public class MySafeClass
{
public delegate MySafeClass CreateMySaveClassDelegate();
private MySafeClass()
{
}
private static MySafeClass Create()
{
return new MySafeClass();
}
public static void Load()
{
List<MySafeClass> instances =
ClassCreator.CreateMySafeClass(new CreateMySaveClassDelegate(Create));
}
}
}
//Class creator
using System;
using System.Collections.Generic;
namespace DelegateCreation
{
public class ClassCreator
{
public static List<MySafeClass>
CreateMySafeClass(MySafeClass.CreateMySaveClassDelegate myDelegate)
{
List<MySafeClass> returnList = new List<MySafeClass>();
returnList.Add(myDelegate.Invoke());
return returnList;
}
}
}
I need to be able to create it externally but only by classes that I define.
To accomplish this I thought of the following process:
Define a delegate that returns an instance of my class.
Pass this an instance of this delegate to any classes that I want to be
able to create instances of myclass.
i.e.
//Safe class
using System;
using System.Collections.Generic;
using System.Text;
namespace DelegateCreation
{
public class MySafeClass
{
public delegate MySafeClass CreateMySaveClassDelegate();
private MySafeClass()
{
}
private static MySafeClass Create()
{
return new MySafeClass();
}
public static void Load()
{
List<MySafeClass> instances =
ClassCreator.CreateMySafeClass(new CreateMySaveClassDelegate(Create));
}
}
}
//Class creator
using System;
using System.Collections.Generic;
namespace DelegateCreation
{
public class ClassCreator
{
public static List<MySafeClass>
CreateMySafeClass(MySafeClass.CreateMySaveClassDelegate myDelegate)
{
List<MySafeClass> returnList = new List<MySafeClass>();
returnList.Add(myDelegate.Invoke());
return returnList;
}
}
}