// C# is a strongly typed language. This means that when the program is compiled the compiler ensures that all actions that are performed are valid in the context of // the types that have been defined in the program.As an example, if a class does not contain a method with a particular name, the C# compiler will refuse to // generate a call to that method.As a way of making sure that C# programs are valid at the time that they are executed, strong typing works very well.Such a // strong typing regime, however, can cause problems when a C# program is required to interact with systems that do not have their origins in C# code. Such // situations arise when using Common Object Model(COM) interop, the Document Object Model(DOM), working with objects generated by C# // reflection, or when interworking with dynamic languages such as JavaScript. In these situations, you need a way to force the compiler to interact with // objects for which the strong typing information that is generated from compiled C# is not available. The keyword dynamic is used to identify items for which // the C# compiler should suspend static type checking. The compiler will then generate code that works with the items as described, without doing static // checking to make sure that they are valid.Note that this doesn’t mean that a program using dynamic objects will always work; if the description is incorrect // the program will fail at run time. public void DynamicTypes() { MessageDisplay m = new MessageDisplay(); m.Display("Hello"); dynamic d = new MessageDisplay(); d.MethodThatDoesNotExist("Hello"); //This program will compile, but when the program is executed an exception will be generated when the method is called. // This aspect of the dynamic keyword makes it possible to interact with objects that have behaviors, but not the C# type information that the C# compiler would // normally use to ensure that any interaction is valid. }
public static int Main(string[] args) { // Create SettingsService early so we know the trace level right at the start SettingsService settingsService = new SettingsService(); InternalTrace.Initialize("nunit-gui_%p.log", (InternalTraceLevel)settingsService.GetSetting("Options.InternalTraceLevel", InternalTraceLevel.Default)); log.Info("Starting NUnit GUI"); GuiOptions guiOptions = new GuiOptions(args); GuiAttachedConsole attachedConsole = null; if (guiOptions.console) { log.Info("Creating attached console"); attachedConsole = new GuiAttachedConsole(); } if (guiOptions.help) { MessageDisplay.Display(guiOptions.GetHelpText()); return(0); } if (!guiOptions.Validate()) { string message = "Error in command line"; MessageDisplay.Error(message + Environment.NewLine + Environment.NewLine + guiOptions.GetHelpText()); log.Error(message); return(2); } if (guiOptions.cleanup) { log.Info("Performing cleanup of shadow copy cache"); DomainManager.DeleteShadowCopyPath(); return(0); } if (!guiOptions.NoArgs) { if (guiOptions.lang != null) { log.Info("Setting culture to " + guiOptions.lang); Thread.CurrentThread.CurrentUICulture = new CultureInfo(guiOptions.lang); } } try { // Add Standard Services to ServiceManager log.Info("Adding Services"); ServiceManager.Services.AddService(settingsService); ServiceManager.Services.AddService(new DomainManager()); ServiceManager.Services.AddService(new RecentFilesService()); ServiceManager.Services.AddService(new ProjectService()); ServiceManager.Services.AddService(new TestLoader(new GuiTestEventDispatcher())); ServiceManager.Services.AddService(new AddinRegistry()); ServiceManager.Services.AddService(new AddinManager()); ServiceManager.Services.AddService(new TestAgency()); // Initialize Services log.Info("Initializing Services"); ServiceManager.Services.InitializeServices(); } catch (Exception ex) { MessageDisplay.FatalError("Service initialization failed.", ex); log.Error("Service initialization failed", ex); return(2); } // Create container in order to allow ambient properties // to be shared across all top-level forms. log.Info("Initializing AmbientProperties"); AppContainer c = new AppContainer(); AmbientProperties ambient = new AmbientProperties(); c.Services.AddService(typeof(AmbientProperties), ambient); log.Info("Constructing Form"); NUnitForm form = new NUnitForm(guiOptions); c.Add(form); try { log.Info("Starting Gui Application"); Application.Run(form); log.Info("Application Exit"); } catch (Exception ex) { log.Error("Gui Application threw an excepion", ex); throw; } finally { log.Info("Stopping Services"); ServiceManager.Services.StopAllServices(); } if (attachedConsole != null) { Console.WriteLine("Press Enter to exit"); Console.ReadLine(); attachedConsole.Close(); } log.Info("Exiting NUnit GUI"); InternalTrace.Close(); return(0); }
private static void dynamics() { dynamic display = new MessageDisplay("Hello World"); display.Display(); }