-
Notifications
You must be signed in to change notification settings - Fork 0
second test #7
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
second test #7
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| | ||
| Microsoft Visual Studio Solution File, Format Version 12.00 | ||
| # Visual Studio Version 17 | ||
| VisualStudioVersion = 17.13.35806.99 d17.13 | ||
| MinimumVisualStudioVersion = 10.0.40219.1 | ||
| Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "secondTestTask", "secondTestTask\secondTestTask.csproj", "{DF153825-073C-4562-891D-52EA5714C96E}" | ||
| EndProject | ||
| Global | ||
| GlobalSection(SolutionConfigurationPlatforms) = preSolution | ||
| Debug|Any CPU = Debug|Any CPU | ||
| Release|Any CPU = Release|Any CPU | ||
| EndGlobalSection | ||
| GlobalSection(ProjectConfigurationPlatforms) = postSolution | ||
| {DF153825-073C-4562-891D-52EA5714C96E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU | ||
| {DF153825-073C-4562-891D-52EA5714C96E}.Debug|Any CPU.Build.0 = Debug|Any CPU | ||
| {DF153825-073C-4562-891D-52EA5714C96E}.Release|Any CPU.ActiveCfg = Release|Any CPU | ||
| {DF153825-073C-4562-891D-52EA5714C96E}.Release|Any CPU.Build.0 = Release|Any CPU | ||
| EndGlobalSection | ||
| GlobalSection(SolutionProperties) = preSolution | ||
| HideSolutionNode = FALSE | ||
| EndGlobalSection | ||
| GlobalSection(ExtensibilityGlobals) = postSolution | ||
| SolutionGuid = {8ECE761C-D09E-4972-B97E-9C9949E9EE17} | ||
| EndGlobalSection | ||
| EndGlobal |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| // See https://aka.ms/new-console-template for more information | ||
|
Check warning on line 1 in secondTestTask/secondTestTask/Program.cs
|
||
| Console.WriteLine("Hello, World!"); | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,297 @@ | ||
| namespace SecondTestTask; | ||
|
Check warning on line 1 in secondTestTask/secondTestTask/Reflector.cs
|
||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Тут вот StyleCop дело говорит, стоило поправить его замечания сразу, это же быстро |
||
|
|
||
| using System.Reflection; | ||
| using System.Runtime.Intrinsics.Arm; | ||
| using System.Text; | ||
|
|
||
| public class Reflector | ||
| { | ||
| public void PrintStructure(Type someClass) | ||
| { | ||
| ArgumentNullException.ThrowIfNull(someClass); | ||
| var fileName = $"{ClearClassName(someClass)}.cs"; | ||
|
Check warning on line 12 in secondTestTask/secondTestTask/Reflector.cs
|
||
| //File.WriteAllText(fileName, GenerateCode(someClass), Encoding.UTF8); | ||
|
Check warning on line 13 in secondTestTask/secondTestTask/Reflector.cs
|
||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. :( |
||
| } | ||
|
|
||
| public void DiffClasses(Type a, Type b) | ||
| { | ||
| if (a == null || b == null) | ||
| { | ||
| throw new ArgumentNullException(); | ||
| } | ||
|
Comment on lines
+18
to
+21
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Это лучше двумя отдельными ThrowIfNull |
||
|
|
||
| if (!a.IsClass || !b.IsClass) | ||
| { | ||
| throw new ArgumentException("DiffClasses поддерживает только классы)"); | ||
| } | ||
|
|
||
| Console.WriteLine($"---Сравнение {a.FullName} и {b.FullName} --- "); | ||
|
|
||
| //DiffFields(a, b); | ||
|
Check warning on line 30 in secondTestTask/secondTestTask/Reflector.cs
|
||
| //DiffMethods(a, b); | ||
|
Check warning on line 31 in secondTestTask/secondTestTask/Reflector.cs
|
||
|
Comment on lines
+30
to
+31
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. :( |
||
| } | ||
|
|
||
| private string ClearClassName(Type T) => T.Name.Split('`')[0]; | ||
|
Check warning on line 34 in secondTestTask/secondTestTask/Reflector.cs
|
||
|
|
||
|
Check warning on line 35 in secondTestTask/secondTestTask/Reflector.cs
|
||
|
|
||
| private string GenerateTypeFile(Type type) | ||
| { | ||
| var builder = new StringBuilder(); | ||
| builder.AppendLine($"using System;"); | ||
| builder.AppendLine($"using System.Collections.Generic;"); | ||
|
|
||
| if (!string.IsNullOrWhiteSpace(type.Namespace)) | ||
| { | ||
| builder.AppendLine($"namespace {type.Namespace};"); | ||
| builder.AppendLine(); | ||
| } | ||
|
|
||
| builder.Append(this.BuildClassCode(type, "")); | ||
| return builder.ToString(); | ||
| } | ||
|
|
||
| private string BuildClassCode(Type type, string indent) | ||
| { | ||
| var sb = new StringBuilder(); | ||
|
|
||
| var access = GetTypeAccess(type); | ||
| var isInterface = type.IsInterface; | ||
| var mods = new List<string> { access }; | ||
| if (type.IsAbstract && type.IsSealed) | ||
| { | ||
| mods.Add("static"); | ||
| } | ||
| else if (type.IsAbstract) | ||
| { | ||
| mods.Add("abstract"); | ||
| } | ||
| else if (type.IsSealed) | ||
| { | ||
| mods.Add("sealed"); | ||
| } | ||
|
|
||
| var name = TypeNameForDeclaration(type); | ||
| var inheritance = GetInheritanceList(type); | ||
| sb.AppendLine($"{indent}{string.Join(" ", mods)} class {name}{inheritance}"); | ||
| sb.AppendLine($"{indent}{{"); | ||
|
|
||
| var fields = type.GetFields(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | | ||
| BindingFlags.Static | BindingFlags.DeclaredOnly).Where(x => !x.IsSpecialName).OrderBy(x => x.Name).ToArray(); | ||
| foreach (var f in fields) | ||
| { | ||
| sb.AppendLine($"{indent} {FieldDeclaration(f)}"); | ||
| } | ||
|
|
||
| if (fields.Length > 0) | ||
| { | ||
| sb.AppendLine(); | ||
| } | ||
|
|
||
| var methods = type.GetMethods(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | | ||
| BindingFlags.Static | BindingFlags.DeclaredOnly).Where(m=> !m.IsSpecialName).OrderBy(m => m.Name).ToArray(); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Конструкторы требуют особого внимания |
||
|
|
||
| foreach (var m in methods) | ||
| { | ||
| sb.AppendLine($"{indent} {MethodDeclaration(m)}"); | ||
| sb.AppendLine($"{indent} {{"); | ||
| sb.AppendLine($"{indent} throw null;"); | ||
| sb.AppendLine($"{indent} }}"); | ||
| sb.AppendLine(); | ||
| } | ||
|
|
||
| var nestedClasses = type.GetNestedTypes(BindingFlags.Public | BindingFlags.NonPublic).Where(nt => nt.IsClass).OrderBy(nt => nt.Name).ToArray(); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Тут стоило проверить, что это не автогенерённый вложенный класс (они именуются с |
||
| foreach (var nt in nestedClasses) | ||
| { | ||
| sb.AppendLine(BuildClassCode(nt, indent + " ")); | ||
| sb.AppendLine(); | ||
| } | ||
|
|
||
| sb.AppendLine($"{indent}}}"); | ||
| return sb.ToString(); | ||
| } | ||
|
|
||
| private string GetTypeAccess(Type t) | ||
| { | ||
| if (!t.IsNested) | ||
| { | ||
| return t.IsPublic ? "public" : "internal"; | ||
| } | ||
|
|
||
| if (t.IsNestedPublic) | ||
| { | ||
| return "public"; | ||
| } | ||
|
|
||
| if (t.IsNestedFamily) | ||
| { | ||
| return "protected"; | ||
| } | ||
|
|
||
| if (t.IsNestedFamORAssem) | ||
| { | ||
| return "protected internal"; | ||
| } | ||
|
|
||
| if (t.IsNestedAssembly) | ||
| { | ||
| return "internal"; | ||
| } | ||
|
|
||
| return "private"; | ||
| } | ||
|
|
||
| private string FieldDeclaration(FieldInfo f) | ||
| { | ||
| var mods = new List<string> { GetMemberAccess(f) }; | ||
| if (f.IsStatic) mods.Add("static"); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ещё бывают всякие readonly, const и т.п. |
||
|
|
||
| return $"{string.Join(" ", mods)} {TypeName(f.FieldType)} {f.Name}"; | ||
| } | ||
|
|
||
| private string MethodDeclaration(MethodInfo m) | ||
| { | ||
| var mods = new List<string> { GetMemberAccess(m) }; | ||
| if (m.IsStatic) | ||
| { | ||
| mods.Add("static"); | ||
| } | ||
|
|
||
| var ret = TypeName(m.ReturnType); | ||
|
|
||
| var gen = m.IsGenericMethodDefinition | ||
| ? $"<{string.Join(", ", m.GetGenericArguments().Select(a => a.Name))}>" | ||
| : ""; | ||
|
|
||
| var pars = string.Join(", ", m.GetParameters().Select(ParameterDeclaration)); | ||
|
|
||
| return $"{string.Join(" ", mods)} {ret} {m.Name}{gen}({pars})"; | ||
| } | ||
|
|
||
| private string ParameterDeclaration(ParameterInfo p) | ||
| { | ||
| var prefix = ""; | ||
| if (p.ParameterType.IsByRef) | ||
| prefix = p.IsOut ? "out " : "ref "; | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 |
||
|
|
||
| var t = p.ParameterType.IsByRef ? p.ParameterType.GetElementType()! : p.ParameterType; | ||
| return $"{prefix}{TypeName(t)} {p.Name}"; | ||
| } | ||
|
|
||
| private string GetMemberAccess(MethodInfo m) | ||
| { | ||
| if (m.IsPublic) | ||
| { | ||
| return "public"; | ||
| } | ||
|
|
||
| if (m.IsFamily) | ||
| { | ||
| return "protected"; | ||
| } | ||
|
|
||
| if (m.IsFamilyOrAssembly) | ||
| { | ||
| return "protected internal"; | ||
| } | ||
|
|
||
| if (m.IsAssembly) | ||
| { | ||
| return "internal"; | ||
| } | ||
|
|
||
| return "private"; | ||
| } | ||
|
|
||
| private string TypeNameForDeclaration(Type t) | ||
| { | ||
| var name = ClearClassName(t); | ||
|
|
||
| if (t.IsGenericTypeDefinition) | ||
| { | ||
| var args = string.Join(", ", t.GetGenericArguments().Select(a => a.Name)); | ||
| return $"{name}<{args}>"; | ||
| } | ||
|
|
||
| return name; | ||
| } | ||
|
|
||
| private string TypeName(Type t) | ||
| { | ||
| if (t.IsGenericParameter) return t.Name; | ||
|
|
||
| if (t == typeof(void)) | ||
| { | ||
| return "void"; | ||
| } | ||
|
|
||
| if (t == typeof(int)) | ||
| { | ||
| return "int"; | ||
| } | ||
|
|
||
| if (t == typeof(string)) | ||
| { | ||
| return "string"; | ||
| } | ||
|
|
||
| if (t == typeof(bool)) return "bool"; | ||
| if (t == typeof(double)) return "double"; | ||
| if (t == typeof(float)) return "float"; | ||
| if (t == typeof(long)) return "long"; | ||
| if (t == typeof(byte)) return "byte"; | ||
| if (t == typeof(char)) return "char"; | ||
| if (t == typeof(decimal)) return "decimal"; | ||
| if (t == typeof(object)) return "object"; | ||
|
|
||
| if (t.IsArray) | ||
| return TypeName(t.GetElementType()!) + "[]"; | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 |
||
|
|
||
| if (t.IsGenericType) | ||
| { | ||
| var name = t.Name.Split('`')[0]; | ||
| var args = string.Join(", ", t.GetGenericArguments().Select(TypeName)); | ||
| return $"{name}<{args}>"; | ||
| } | ||
|
|
||
| return t.Name; | ||
| } | ||
|
|
||
| private string GetInheritanceList(Type t) | ||
| { | ||
| var list = new List<string>(); | ||
|
|
||
| if (t.BaseType != null && t.BaseType != typeof(object)) | ||
| list.Add(TypeName(t.BaseType)); | ||
|
|
||
| list.AddRange(t.GetInterfaces().Select(TypeName)); | ||
|
|
||
| list = list.Distinct().ToList(); | ||
| return list.Count == 0 ? "" : " : " + string.Join(", ", list); | ||
| } | ||
|
Check warning on line 270 in secondTestTask/secondTestTask/Reflector.cs
|
||
| private string GetMemberAccess(FieldInfo f) | ||
|
Check warning on line 271 in secondTestTask/secondTestTask/Reflector.cs
|
||
| { | ||
| if (f.IsPublic) | ||
| { | ||
| return "public"; | ||
| } | ||
|
|
||
| if (f.IsFamily) | ||
| { | ||
| return "protected"; | ||
| } | ||
|
|
||
| if (f.IsFamilyOrAssembly) | ||
| { | ||
| return "protected internal"; | ||
| } | ||
|
|
||
| if (f.IsAssembly) | ||
| { | ||
| return "internal"; | ||
| } | ||
|
|
||
| return "private"; | ||
| } | ||
|
|
||
| } | ||
|
Check warning on line 296 in secondTestTask/secondTestTask/Reflector.cs
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| <Project Sdk="Microsoft.NET.Sdk"> | ||
|
|
||
| <PropertyGroup> | ||
| <OutputType>Exe</OutputType> | ||
| <TargetFramework>net9.0</TargetFramework> | ||
| <ImplicitUsings>enable</ImplicitUsings> | ||
| <Nullable>enable</Nullable> | ||
| </PropertyGroup> | ||
|
|
||
| <ItemGroup> | ||
| <PackageReference Include="StyleCop.Analyzers" Version="1.2.0-beta.556"> | ||
| <PrivateAssets>all</PrivateAssets> | ||
| <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets> | ||
| </PackageReference> | ||
| </ItemGroup> | ||
|
|
||
| </Project> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
:(