You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
So this is more or less a collection of things i found missing and i am not sure if these are even possible/how to implement them.
Downcasting dynamic objects. Found this issue, when i played around with the DynamicObj.ofJson function i added. It will return a DynamicObj which cannot be downcaster to any type inheriten DynamicObj. The workaround is to use CopyDynamicPropertiesTo on the inerhiting type.
typeLogger()=inherit DynamicObj()letlogger= Logger()
logger.SetValue ("key","value")letdynObj:DynamicObj = DynamicObj()
dynObj.SetValue ("key","value")// upcastletd2= logger :> DynamicObj // upcast works without issue
d2.TryGetValue "key"// Some "value"// downcast
dynObj :?> Logger
// Error: System.InvalidCastException: Unable to cast object of type 'DynamicObj.DynamicObj' to type 'Logger'.//workaround
dynObj.CopyDynamicPropertiesTo logger
Cast DynamicObj to (anonymous) record type. This could be especially nice for the TryGetTypedValue member.
typeMyRecordType={|
key:string|}letdynObj:DynamicObj = DynamicObj()
dynObj.SetValue ("key","value")
dynObj :?> MyRecordType
// Type constraint mismatch. The type 'MyRecordType' is not compatible with type 'DynamicObj
The text was updated successfully, but these errors were encountered:
Soo just made up this poc example, the following could be used in the style of DynamicObj.TryGetTypedValue.
#r "nuget: DynamicObj, 1.0.1"openDynamicObjtypeMyType={
Key:string
Port:int}openFSharp.ReflectiontypeLogger()=inherit DynamicObj()member inlinethis.getAsRecordType<'a>()=lett= typeof<'a>match FSharpType.IsRecord(t)with|true->letfieldValues=
FSharpType.GetRecordFields(t)|> Array.map (fun x ->letvo= this.TryGetValue x.Name
if vo.IsNone then failwithf "Field %A does not exist." x.Name else vo.Value
)
FSharpValue.MakeRecord(t, fieldValues):?> 'a
|false->
failwithf "%A is not a record type." t.FullName
letdynObj= Logger()
dynObj.SetValue("Key","Test Value")
dynObj.SetValue("Port",8080)letres= dynObj.getAsRecordType<MyType>()
So this is more or less a collection of things i found missing and i am not sure if these are even possible/how to implement them.
DynamicObj
. The workaround is to useCopyDynamicPropertiesTo
on the inerhiting type.TryGetTypedValue
member.The text was updated successfully, but these errors were encountered: