public W GetWherigoObject <W>(int objIndex) where W : WherigoObject { // Gets the object. WherigoObject wo = GetWherigoObjectCore(objIndex, dontFailIfBadArg: true); // Checks if the wherigo object is of the right type. if (wo != null && !(wo is W)) { throw new InvalidOperationException(String.Format("The object with index {0} is known to be of type {1}, not {2} as requested.", objIndex, wo.GetType().FullName, typeof(W).FullName)); } // Returns the object, cast to the proper type. return((W)wo); }
public W CreateWherigoObject <W>(params object[] arguments) where W : WherigoObject { Type wherigoType = typeof(W); WherigoObject obj = null; // Conforms parameters. List <object> args = new List <object>(); if (arguments != null) { args.AddRange(arguments); } // Creates the object if the requested type is supported. if (wherigoType == typeof(ZonePoint)) { obj = CreateZonePoint(args); } else if (wherigoType == typeof(Distance)) { obj = CreateDistance(args); } else { throw new NotSupportedException("Requested type is not supported."); } // Checks the type and returns the object. if (!(obj is W)) { throw new InvalidOperationException(String.Format("Requested type {0}, got type {1}.", wherigoType.Name, obj.GetType().Name)); } return((W)obj); }