Copying and Pasting cs Code

In cs, like in almost any computer programming language, reading data from a file can be tricky. You add extra lines of code to tell the computer what to do. Sometimes you can copy and paste these lines from other peoples’ code.

For example, you can follow the pattern in this listing:

         private IList GetCustomImporterInstances()
         {
             // Report an error for ICustomTiledImporter classes that don't have the CustomTiledImporterAttribute
             var errorTypes = from a in AppDomain.CurrentDomain.GetAssemblies()
                              from t in a.GetTypes()
                              where typeof(ICustomTiledImporter).IsAssignableFrom(t)
                              where !t.IsAbstract
                              where Attribute.GetCustomAttribute(t, typeof(CustomTiledImporterAttribute)) == null
                              select t;
             foreach (var t in errorTypes)
             {
                 Debug.LogError(String.Format("ICustomTiledImporter type '{0}' is missing CustomTiledImporterAttribute", t));
             }

             // Find all the types with the CustomTiledImporterAttribute, instantiate them, and give them a chance to customize our prefab
             var types = from a in AppDomain.CurrentDomain.GetAssemblies()
                         from t in a.GetTypes()
                         where typeof(ICustomTiledImporter).IsAssignableFrom(t)
                         where !t.IsAbstract
                         from attr in Attribute.GetCustomAttributes(t, typeof(CustomTiledImporterAttribute))
                         let custom = attr as CustomTiledImporterAttribute
                         orderby custom.Order
                         select t;

             var instances = types.Select(t => (ICustomTiledImporter)Activator.CreateInstance(t));
             return instances.ToList();
         }