Page 1 of 1

Reflection API in portable class libraries

Posted: Sat Feb 03, 2018 4:06 am
by Ajcek
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.

Re: Reflection API in portable class libraries

Posted: Mon Feb 05, 2018 6:23 am
by JS-Support @Userware
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