/// <summary> /// Creates a typed object that is a direct child of the iCalendar itself. Generally, /// you would invoke this method to create an Event, Todo, Journal, TimeZone, FreeBusy, /// or other base component type. /// </summary> /// <example> /// To create an event, use the following: /// <code> /// iCalendar iCal = new iCalendar(); /// /// Event evt = iCal.Create<Event>(); /// </code> /// /// This creates the event, and adds it to the Events list of the iCalendar. /// </example> /// <typeparam name="T">The type of object to create</typeparam> /// <returns>An object of the type specified</returns> public T Create <T>() { if (m_ComponentBaseCreate == null) { throw new ArgumentException("Create() cannot be called without a valid ComponentBase Create() method attached"); } // Create a dummy object with a null parent iCalObject obj = null; object t = Activator.CreateInstance(typeof(T), obj); if (t is iCalObject) { iCalObject ico = (iCalObject)t; // Create the type of object that we're looking for... iCalObject resultObject = m_ComponentBaseCreate.Invoke(null, new object[] { this, ico.Name }) as iCalObject; resultObject.CreateInitialize(); return((T)(object)resultObject); } else { return(default(T)); } }
/// <summary> /// Creates a typed object that is a direct child of the iCalendar itself. Generally, /// you would invoke this method to create an Event, Todo, Journal, TimeZone, FreeBusy, /// or other base component type. /// </summary> /// <example> /// To create an event, use the following: /// <code> /// iCalendar iCal = new iCalendar(); /// /// Event evt = iCal.Create<Event>(); /// </code> /// /// This creates the event, and adds it to the Events list of the iCalendar. /// </example> /// <typeparam name="T">The type of object to create</typeparam> /// <returns>An object of the type specified</returns> public T Create <T>() where T : iCalObject { if (m_ComponentBaseCreate == null) { throw new ArgumentException("Create() cannot be called without a valid ComponentBase Create() method attached"); } ConstructorInfo ci = typeof(T).GetConstructor(new Type[] { typeof(iCalObject) }); if (ci != null) { // Create a dummy object with a null parent iCalObject ico = ci.Invoke(new object[] { null }) as iCalObject; if (ico != null) { iCalObject resultObject = m_ComponentBaseCreate.Invoke(null, new object[] { this, ico.Name }) as iCalObject; resultObject.CreateInitialize(); return((T)(object)resultObject); } } return(default(T)); }