Let's say there is a graphical API G that does not want to depend on any external mathematical library and therefore defines all its vector parameters as open structures ( as SharpDX does with its raw types ).
Let there also be a mathematical library M.
The developer wants to provide implicit operators in assembly A for transferring between the full type from the mathematical library M to the original type from the graphical API G ( another selection from SharpDX, but in this case the operators are defined in M ββinstead of A ). Note that both types have the same memory representation.
Afaik, in C # there is no means to provide such operators. The closest thing that comes to mind is extension methods, but the user still needs to call an additional method to convert.
Is this possible or are there any patterns of such operators that are added through IL rewriting? From my limited knowledge, I would think that this is possible, but I would be grateful for additional feedback on this issue.
Thank!
You can do this by rewriting the IL of one of the two libraries, in particular, you add an implicit statement to the type (and in the process also add a dependency on the other library).
, : , (.. NuGet), , , IL-.
, Foo(bar) Foo(bar.ToBaz()).
Foo(bar)
Foo(bar.ToBaz())
, . , . ? . .
// External Assemblies A and B define the following types: // Assembly A public class X { public string Name { get; set; } public static void SomeFunction(X item) { Console.WriteLine($"X.{item.Name}"); } } // Assembly B public class Y { public string Name { get; set; } public static void SomeFunction(Y item) { Console.WriteLine($"Y.{item.Name}"); } } // We would create an implicit intermediary in intermediate assembly C // Assembly C public class Z { public string Name { get; set; } public static implicit operator Z(X input) => new Z { Name = input.Name }; public static implicit operator X(Z input) => new X { Name = input.Name }; public static implicit operator Z(Y input) => new Z { Name = input.Name }; public static implicit operator Y(Z input) => new Y { Name = input.Name }; } public void PushToX(Z thing) => X.SomeFunction(thing); public void PushToY(Z thing) => Y.SomeFunction(thing); public void DoThing(Z thing) { Console.WriteLine(thing.Name); } public void Main() { var a = new X { Name = "A" }; var b = new Y { Name = "B" }; DoThing(a); DoThing(b); PushToX(a); PushToY(a); PushToX(b); PushToY(b); //A //B //X.A //Y.A //X.B //Y.B }
Source: https://habr.com/ru/post/1654378/More articles:The relationship between dwPageSize and dwAllocationGranularity - c ++The term "Scaffold-DbContext" is not recognized as the cmdlet name - visual-studioHow to check if an object method is called inside a completion handler block using OCMock? - iosHow to get Mac address for Android and Android with 6.0 or higher in C #? - androidWhy do flexible items wrap instead of shrink? - htmlGradle build dangling when jackOptions is enabled for Java 1.8 - javaUpgrade your Android project to use java 8 and get your GC limit exceeded - javaHow to force Inno Setup to dynamically install the installation folder - installationTask.Yield (); SyncAction (); vs Task.Run (() => SyncAction ()); - c #Is it possible to specify a ROOT element in a select statement using FOR XML PATH using a variable? - xmlAll Articles