OpenJudgeSystem icon indicating copy to clipboard operation
OpenJudgeSystem copied to clipboard

Some source code examples (OOP Exam) work on old system but not on new one

Open ivaylokenov opened this issue 10 years ago • 2 comments

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.CodeDom.Compiler;
using Microsoft.CSharp;
using System.Reflection;
 
namespace SoftwareAcademy
{
        public abstract class Course : ICourse
        {
            private string name;
            private List<string> topics = new List<string>();
            private ITeacher currentTeacher;
 
            public string Name
            {
                get
                {
                    return this.name;
                }
                set
                {
                    if (value == null || value == string.Empty)
                    {
                        throw new ArgumentNullException("Value cannot be null or empty!");
                    }
                    this.name = value;
                }
            }
 
            public ITeacher Teacher
            {
                get
                {
                    return this.currentTeacher;
                }
                set
                {
                    this.currentTeacher = value;
                }
            }
 
            public void AddTopic(string topic)
            {
                if (topic == null || topic == string.Empty)
                {
                    throw new ArgumentNullException("Value cannot be null or empty!");
                }
                this.topics.Add(topic);
            }
 
            protected Course(string name, ITeacher teacher)
            {
                this.Name = name;
                this.Teacher = teacher;
            }
 
            public override string ToString()
            {
                StringBuilder result = new StringBuilder();
 
                result.Append(string.Format("Name={0}", this.Name));
 
                if (this.currentTeacher != null)
                {
                    result.Append(string.Format("; Teacher={0}", this.currentTeacher.Name));
                }
 
                if (this.topics.Count != 0)
                {
                    result.Append("; Topics=[");
 
                    foreach (var topic in topics)
                    {
                        result.Append(string.Format("{0}, ", topic));
                    }
 
                    result.Remove(result.Length - 2, 2);
                    result.Append("]");
                }
 
                return result.ToString();
            }
        }
}
 
namespace SoftwareAcademy
{
        public class LocalCourse : Course, ILocalCourse
        {
            private string lab;
 
            public string Lab
            {
                get
                {
                    return this.lab;
                }
                set
                {
                    if (value == null || value == string.Empty)
                    {
                        throw new ArgumentNullException("Value cannot be null or empty");
                    }
                    this.lab = value;
                }
            }
 
            public LocalCourse(string name, ITeacher teacher, string lab)
                : base(name, teacher)
            {
                this.Lab = lab;
            }
 
            public override string ToString()
            {
                StringBuilder result = new StringBuilder();
 
                result.Append(this.GetType().Name + ": ");
 
                result.Append(base.ToString());
 
                result.Append(string.Format("; Lab={0}", this.Lab));
 
                return result.ToString();
            }
        }
}
 
namespace SoftwareAcademy
{
        class OffsiteCourse : Course, IOffsiteCourse
        {
            private string town;
 
            public string Town
            {
                get
                {
                    return this.town;
                }
                set
                {
                    if (value == null || value == string.Empty)
                    {
                        throw new ArgumentNullException("Value cannot be null or empty");
                    }
                    this.town = value;
                }
            }
 
            public OffsiteCourse(string name, ITeacher teacher, string town)
                : base(name, teacher)
            {
                this.Town = town;
            }
 
            public override string ToString()
            {
                StringBuilder result = new StringBuilder();
 
                result.Append(this.GetType().Name + ": ");
 
                result.Append(base.ToString());
 
                result.Append(string.Format("; Town={0}", this.Town));
 
                return result.ToString();
            }
        }
}
 
 
namespace SoftwareAcademy
{
        public interface ITeacher
        {
            string Name { get; set; }
            void AddCourse(ICourse course);
            string ToString();
        }
 
        public interface ICourse
        {
            string Name { get; set; }
            ITeacher Teacher { get; set; }
            void AddTopic(string topic);
            string ToString();
        }
 
        public interface ILocalCourse : ICourse
        {
            string Lab { get; set; }
        }
 
        public interface IOffsiteCourse : ICourse
        {
            string Town { get; set; }
        }
 
        public interface ICourseFactory
        {
            ITeacher CreateTeacher(string name);
            ILocalCourse CreateLocalCourse(string name, ITeacher teacher, string lab);
            IOffsiteCourse CreateOffsiteCourse(string name, ITeacher teacher, string town);
        }
 
        public class CourseFactory : ICourseFactory
        {
            public ITeacher CreateTeacher(string name)
            {
                return new Teacher(name);
            }
 
            public ILocalCourse CreateLocalCourse(string name, ITeacher teacher, string lab)
            {
                return new LocalCourse(name, teacher, lab);
            }
 
            public IOffsiteCourse CreateOffsiteCourse(string name, ITeacher teacher, string town)
            {
                return new OffsiteCourse(name, teacher, town);
            }
        }
 
        public class SoftwareAcademyCommandExecutor
        {
            static void Main()
            {
                string csharpCode = ReadInputCSharpCode();
                CompileAndRun(csharpCode);
            }
 
            private static string ReadInputCSharpCode()
            {
                StringBuilder result = new StringBuilder();
                string line;
                while ((line = Console.ReadLine()) != "")
                {
                    result.AppendLine(line);
                }
                return result.ToString();
            }
 
            static void CompileAndRun(string csharpCode)
            {
                // Prepare a C# program for compilation
                string[] csharpClass =
                {
                    @"using System;
                      using SoftwareAcademy;
 
                      public class RuntimeCompiledClass
                      {
                         public static void Main()
                         {"
                            + csharpCode + @"
                         }
                      }"
                };
 
                // Compile the C# program
                CompilerParameters compilerParams = new CompilerParameters();
                compilerParams.GenerateInMemory = true;
                compilerParams.TempFiles = new TempFileCollection(".");
                compilerParams.ReferencedAssemblies.Add("System.dll");
                compilerParams.ReferencedAssemblies.Add(Assembly.GetExecutingAssembly().Location);
                CSharpCodeProvider csharpProvider = new CSharpCodeProvider();
                CompilerResults compile = csharpProvider.CompileAssemblyFromSource(
                    compilerParams, csharpClass);
 
                // Check for compilation errors
                if (compile.Errors.HasErrors)
                {
                    string errorMsg = "Compilation error: ";
                    foreach (CompilerError ce in compile.Errors)
                    {
                        errorMsg += "\r\n" + ce.ToString();
                    }
                    throw new Exception(errorMsg);
                }
 
                // Invoke the Main() method of the compiled class
                Assembly assembly = compile.CompiledAssembly;
                Module module = assembly.GetModules()[0];
                Type type = module.GetType("RuntimeCompiledClass");
                MethodInfo methInfo = type.GetMethod("Main");
                methInfo.Invoke(null, null);
            }
        }
}
 
namespace SoftwareAcademy
{
        public class Teacher : ITeacher
        {
            private string name;
            IList<ICourse> currentCourses = new List<ICourse>();
 
            public string Name
            {
                get
                {
                    return this.name;
                }
                set
                {
                    this.name = value;
                }
            }
 
            public Teacher(string name = null)
            {
                this.Name = name;
            }
 
            public void AddCourse(ICourse course)
            {
                this.currentCourses.Add(course);
            }
 
            public override string ToString()
            {
                StringBuilder result = new StringBuilder();
                result.Append(this.GetType().Name);
                result.Append(": ");
                result.Append(string.Format("Name={0}", this.Name));
 
                if (this.currentCourses.Count != 0)
                {
                    result.Append("; Courses=[");
 
                    foreach (var course in currentCourses)
                    {
                        result.Append(course.Name + ", ");
                    }
 
                    result.Remove(result.Length - 2, 2);
                    result.Append("]");
                }
 
                return result.ToString();
            }
        }
}

ivaylokenov avatar Nov 20 '13 17:11 ivaylokenov

Unhandled Exception: System.UnauthorizedAccessException: Access to the path 'C:\Windows\TEMP\i2nx2z5n.tmp' is denied.
at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
at System.IO.FileStream.Init(String path, FileMode mode, FileAccess access, Int32 rights, Boolean useRights, FileShare share, Int32 bufferSize, FileOptions options, SECURITY_ATTRIBUTES secAttrs, String msgPath, Boolean bFromProxy, Boolean useLongPath, Boolean checkHost)
at System.IO.FileStream..ct

NikolayIT avatar Dec 12 '13 12:12 NikolayIT

May be create special execution strategy with less restrictions and assign this contest to it

NikolayIT avatar Dec 12 '13 15:12 NikolayIT