Comments (8)
Thanks for the idea @yegorpetrov, the proposed solution is OK but the c.Order(orderFunc(c.Type))
call does not work as expected (I am using the latest tool version of 1.4.6), i.e. the reflected/translated code is not in the order it should be...
I made a bit more verbose version of the hierarchy enumeration with a Type dict like so:
public static void Configure(ConfigurationBuilder builder)
{
var types = typeof(Class1).Assembly.GetTypes().ToList();
var orderedTypes = new Dictionary<Type, List<Type>>();
types.ForEach(t => orderedTypes.Add(t, EnumerateHierarchy(t, e => e.BaseType).Reverse().ToList()));
builder.ExportAsClasses(types, c => c.Order(orderedTypes[c.Type].IndexOf(c.Type)));
}
static IEnumerable<T> EnumerateHierarchy<T>(T item, Func<T, T> selector) where T : class
{
do
{
yield return item;
item = selector(item);
}
while (item != default(T));
}
... I tested this separately on three class files (Class1, Class2, Class3) from three different Namespaces like so:
namespace ReinforcedTypings.DifferentNamespaces.Ns1
{
public class Class1
{
public int AnIntegerPropNs1 { get; set; }
public string AStringPropNs1 { get; set; }
}
}
namespace ReinforcedTypings.DifferentNamespaces.Ns2
{
public class Class2 : Class1
{
public int AnIntegerPropNs2 { get; set; }
public string AStringPropNs2 { get; set; }
}
}
namespace ReinforcedTypings.DifferentNamespaces.Ns3
{
public class Class3
{
public int AnIntegerPropNs3 { get; set; }
public Class1 AClass1PropNs3 { get; set; }
}
}
And the tested order I get is:
- Class 3 with order number 1
- Class 2 with order number 2
- Class 1 with order number 1
So either way the Class1 should appear before Class2 which extends it in TypeScript so we shouldn't get the error of having a class used before its declaration, but unfortunately that is not the case.
The output:
from reinforced.typings.
Why dont you use builder.ExportAs...().Order(10)
method or .ExportAsClasses(...,x=>x.Order(...))
?
Please ask questions on StackOverflow
from reinforced.typings.
The problem I see here is that the output is erroneous by default. And it can be cumbersome to figure out and maintain the proper order in a situation with numerous interdependent classes. I kinda expected this framework to order types properly by itself.
Is this out of the scope of the project? Were there attempts to sort it out automatically?
from reinforced.typings.
It is out of scope of project because
- this case is complex to detect and resolve when exporting to multiple files/namespaces plus inheritance of classes in different namespaces plus how it should work with TS modules?
- this case is extremely rare and easily fixable by pulling all the base classes to the top using
.Order
- so according to previous 2 points, ratio of time spent implementing this feature/collected benefits is extremely low
So for now I'm not going to implement that. If you want to implement this feature - feel free to fork & PR. Otherwise please use .Order
from reinforced.typings.
If someone else comes upon this issue, here's one way to "pull all the base classes to the top" in a more or less universal manner:
IEnumerable<T> EnumerateHierarchy<T>(T item, Func<T, T> selector)
{
do yield return item;
while ((item = selector(item)) != default(T));
}
...
IEnumerable<Type> dtoClasses...
Func<Type, int> orderFunc = dtoClasses.SelectMany(
t => EnumerateHierarchy(t, e => e.BaseType).Reverse()).ToList().IndexOf;
cfg.ExportAsClasses(dtoClasses, c => c.Order(orderFunc(c.Type))...
May be quite far from perfect, it's only tested to cover my specific case.
from reinforced.typings.
Thank you, Egor! But it would be much better if you post such kind of question to stackoverflow - more audience coverage there.
I have marked your solution with "faq" label, but still not sure that someone can find it quickly.
from reinforced.typings.
@vmandic I think that's because you have separate namespaces. You can't expect the lower level order to override the higher level one automatically.
from reinforced.typings.
RT. Connecting people.
from reinforced.typings.
Related Issues (20)
- Invalid code generated when C# code inherits from generic
- v1.6.1 does not support .NET 7 HOT 23
- Invalid code generated for explicit interface member implementation
- Export public static properties of static class to enum HOT 2
- Add raw line to output file (for example to disable eslint errors) HOT 3
- Is it possible to generate union?
- RT0999: Input string was not in a correct format.
- Are SDK Projects supported? HOT 3
- MSB6006 "" exited with code -214745073
- Issue when base class is exported as interface HOT 1
- feat: Net8 support HOT 24
- <RtDisable> and <TreatWarningsAsError> not working HOT 1
- Error MSB6006: "" exited with code 1. HOT 2
- The AddImports feature should allow you to select what quotes to use
- Generated Typescript loses information about combined enum flags when using UseString
- Improved compilation of JSDOC
- JSDOC for enums
- Add a custom property to a class HOT 4
- feat: Net9 support
- Empty file generated?
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from reinforced.typings.