示例#1
0
        /// <summary>
        /// Helper function to allow a <see cref="DataSerializer"/> to be implemented as a singleton.  Returns the singleton instance generated by the <see cref="DPSManager"/>
        /// </summary>
        /// <typeparam name="T">The <see cref="Type"/> of the <see cref="DataSerializer"/> to retrieve from the <see cref="DPSManager"/></typeparam>
        /// <returns>The singleton instance generated by the <see cref="DPSManager"/></returns>
        protected static T GetInstance <T>() where T : DataSerializer
        {
            //this forces helper static constructor to be called
            T instance = DPSManager.GetDataSerializer <T>() as T;

            if (instance == null)
            {
                //if the instance is null the type was not added as part of composition
                //create a new instance of T and add it to helper as a serializer
                var construct = typeof(T).GetConstructor(new Type[] { });
                if (construct == null)
                {
                    construct = typeof(T).GetConstructor(BindingFlags.NonPublic | BindingFlags.Instance, null, new Type[0], null);
                }

                if (construct == null)
                {
                    throw new Exception();
                }

                instance = construct.Invoke(new object[] { }) as T;

                DPSManager.AddDataSerializer(instance);
            }

            return(instance as T);
        }
示例#2
0
 static DPSManager()
 {
     instance = new DPSManager();
 }