Reflection API in portable class libraries

Please post public support tickets here. Note: for private support tickets, please send an email to support@cshtml5.com instead.
Ajcek
Posts: 7
Joined: Thu Mar 09, 2017 7:03 am

Reflection API in portable class libraries

Postby Ajcek » Sat Feb 03, 2018 4:06 am

Portable class libraries use a different reflection API than normal .NET libraries.
In portable class library projects Type is divided into Type and TypeInfo. We can use TypeInfo.AsType() to get Type from TypeInfo and Type.GetTypeInfo() to get TypeInfo from Type.
CSHTML5 uses the old approach. There is only one Type class that has methods from both Type and TypeInfo so I have to write different code for CSHTML5 and different for portable .NET library:

#if CSHTML5

var strategyTypes = typeof(MusicXmlParsingStrategy).Assembly.GetTypes().Where(t => t.IsSubclassOf(typeof(MusicXmlParsingStrategy)) && !t.IsAbstract);
List<MusicXmlParsingStrategy> strategies = new List<MusicXmlParsingStrategy>();
foreach (var type in strategyTypes)
{
strategies.Add(Activator.CreateInstance(type) as MusicXmlParsingStrategy);
}
_strategies = strategies.ToArray();
#else
var strategyTypes = typeof(MusicXmlParsingStrategy).GetTypeInfo().Assembly.DefinedTypes.Where(t => t.IsSubclassOf(typeof(MusicXmlParsingStrategy)) && !t.IsAbstract);
List<MusicXmlParsingStrategy> strategies = new List<MusicXmlParsingStrategy>();
foreach (var type in strategyTypes)
{
strategies.Add(Activator.CreateInstance(type.AsType()) as MusicXmlParsingStrategy);
}
_strategies = strategies.ToArray();
#endif


I think it would be useful to implement TypeInfo, GetTypeInfo() and AsType() in CSHTML5 in the following way:
TypeInfo - inherits from Type and doesn't introduce any new members,
GetTypeInfo() - returns TypeInfo from Type,
AsType() - returns Type from TypeInfo.

Thanks to that I would not have to make two versions of code.

JS-Support @Userware
Site Admin
Posts: 894
Joined: Tue Apr 08, 2014 3:42 pm

Re: Reflection API in portable class libraries

Postby JS-Support @Userware » Mon Feb 05, 2018 6:23 am

Thank you very much for the suggestion. The current version of CSHTML5 references the "mscorlib" of the .NET Framework 4.x. We are studying the possibility to reference alternative versions such as the PCL one.

Regards,
JS-Support


Return to “Technical Support”

Who is online

Users browsing this forum: No registered users and 2 guests

 

 

cron