You cannot add methods to the already existing class Convert , it is not designed for possible extensions. But you can include its functionality in your class. To do this, we will add all methods from System.Convert to Myproject.Convert !
Of course, doing it manually is wrong, we will come to the aid of code generation. Let's write the auxiliary project:
using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Reflection; // ... static string Stringify(Type t) => t.FullName; static string Stringify(IEnumerable<Type> tl) => string.Join(", ", tl.Select(Stringify)); static string Stringify(ParameterInfo pi) { if (pi.IsIn || pi.ParameterType.IsByRef || pi.HasDefaultValue) throw new NotSupportedException(); return $"{Stringify(pi.ParameterType)} {pi.Name}"; } static string Stringify(IEnumerable<ParameterInfo> pl) => string.Join(", ", pl.Select(Stringify)); static string StringifyNames(IEnumerable<ParameterInfo> pl) => string.Join(", ", pl.Select(p => p.Name)); static void Main(string[] args) { var methods = typeof(Convert) .GetMethods(BindingFlags.Public | BindingFlags.Static); using (var outf = File.CreateText("Convert.proxy.cs")) { outf.WriteLine("using System.Runtime.CompilerServices;"); outf.WriteLine(); outf.WriteLine("namespace MyProject"); outf.WriteLine("{"); outf.WriteLine(" static partial class Convert"); outf.WriteLine(" {"); outf.WriteLine(); foreach (var method in methods) { outf.WriteLine(" [MethodImpl(MethodImplOptions.AggressiveInlining)]"); outf.Write($" public static {method.ReturnType.FullName} {method.Name}"); if (method.IsGenericMethodDefinition) outf.Write($"<{Stringify(method.GetGenericArguments())}>"); var parameters = method.GetParameters(); outf.WriteLine($"({Stringify(parameters)}) => " + $"System.Convert.{method.Name}({StringifyNames(parameters)});"); outf.WriteLine(); } outf.WriteLine(" }"); outf.WriteLine("}"); } }
This code will generate a file called Convert.proxy.cs , which you connect to the main project:
using System.Runtime.CompilerServices; namespace MyProject { static partial class Convert { [MethodImpl(MethodImplOptions.AggressiveInlining)] public static System.TypeCode GetTypeCode(System.Object value) => System.Convert.GetTypeCode(value); [MethodImpl(MethodImplOptions.AggressiveInlining)] public static System.Boolean IsDBNull(System.Object value) => System.Convert.IsDBNull(value);
and. etc.
Since the class is declared as partial , you can add functions to it without editing the generated text. For example, in addition in another file you can write:
namespace MyProject { static partial class Convert // Π΄ΠΎΠ±Π°Π²Π»ΡΠ΅ΠΌ ΠΌΠ΅ΡΠΎΠ΄Ρ Π² ΠΊΠ»Π°ΡΡ Convert { public static Model ToModel(LongModel LM) { return new Model { id = LM.id, Name = LM.Name }; } } // ΠΈ Ρ. Π΄. }
Everything, now everywhere you can use MyProject.Convert .
longModel.ToModel(), why should we try to add them toConvert? - Grundy