Page 1 of 1

[WORKAROUND] Unimplemented form of generic method parameter: 'T'.

Posted: Tue Feb 06, 2018 11:54 pm
by Ajcek
When I try to run my app there is an error while generating javascript files:

ERROR: C#/XAML for HTML5: JavaScriptGenerator failed: System.Exception: Error occurred while generating javascript for assembly 'Manufaktura.Controls.XamlForHtml5.MusicShadow, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null'. ---> System.Exception: An error occurred while declaring the type 'Manufaktura.Music.Xml.XAttributeHelper' ---> System.Exception: An error occurred while declaring the type 'Manufaktura.Music.Xml.XHelperBase' ---> System.NotImplementedException: Unimplemented form of generic method parameter: 'T'.

I thought that it might be related to nullable generic parameter:
private static T? ParseValue<T>(string value) where T : struct

but it seems that it's not the case.

Can you tell me which form of generic parameter is unsupported here? This is my class:
public abstract class XHelperBase : IXHelper
{
public XHelperExistsResult Exists()
{
return new XHelperExistsResult(ElementExists(), GetObject(), GetValue());
}

public XHelperHasValueResult<string> HasAnyValue()
{
if (!ElementExists()) return new XHelperHasValueResult<string>(null, null, false);
var value = GetValue();
return !string.IsNullOrWhiteSpace(value) ? new XHelperHasValueResult<string>(value, value, true) : new XHelperHasValueResult<string>(null, null, false);
}

public XHelperHasValueResult<T> HasValue<T>(Dictionary<string, T> values)
{
if (ElementExists() && values.ContainsKey(GetValue())) return new XHelperHasValueResult<T>(values[GetValue()], GetValue(), true);
return new XHelperHasValueResult<T>(default(T), GetValue(), false);
}

public XHelperHasValueResult<T> HasValue<T>() where T : struct
{
if (!ElementExists()) return new XHelperHasValueResult<T>(default(T), null, false);
var unparsedValue = GetValue();
var value = ParseValue<T>(unparsedValue);

return value.HasValue ? new XHelperHasValueResult<T>(value.Value, GetValue(), true) : new XHelperHasValueResult<T>(default(T), GetValue(), false);
}

public XHelperHasValueResult<string> HasValue(string s)
{
if (!ElementExists()) return new XHelperHasValueResult<string>(null, null, false);
var value = GetValue();
return !string.IsNullOrWhiteSpace(value) && s == value ? new XHelperHasValueResult<string>(value, GetValue(), true) : new XHelperHasValueResult<string>(null, GetValue(), false);
}

protected abstract bool ElementExists();

protected abstract string GetValue();

protected abstract object GetObject();


private static T? ParseValue<T>(string value) where T : struct
{
if (typeof(T) == typeof(int)) return UsefulMath.TryParseInt(value) as T?;
if (typeof(T) == typeof(double)) return UsefulMath.TryParse(value) as T?;
if (typeof(T) == typeof(float)) return UsefulMath.TryParse(value) as T?;
if (typeof(T) == typeof(DateTime)) return UsefulMath.TryParseDateTime(value) as T?;
throw new NotImplementedException("Type not supported");
}


public XHelperHasValueResult<T> HasValue<T>(Func<Dictionary<string, T>, Dictionary<string, T>> valueFactory)
{
var dict = valueFactory(new Dictionary<string, T>());
return HasValue(dict);
}
}

Thanks in advance.

Re: Unimplemented form of generic method parameter: 'T'.

Posted: Fri Feb 09, 2018 3:43 am
by JS-Support @Userware
Hi,

Thanks for your message.

We are going to look into it and keep you updated asap!

Regards,
JS-Support

Re: Unimplemented form of generic method parameter: 'T'.

Posted: Wed Feb 21, 2018 2:28 am
by JS-Support @Userware
Hi,

We have investigated and isolated the issue.

The following code snippet is apparently not supported by the current version of the JSIL transpiler (when the generic type parameter is passed to a generic method):

Code: Select all

public void MethodThatBlocksJSCompilation<T>()
{
    var v = new Dictionary<string, T>(); //this doesn't work
}


We are going to fix it asap.

In the meantime, there is an easy workaround, which is to instantiate the dictionary using the "ToDictionary" Linq method, as shown below:

Code: Select all

List<KeyValuePair<string, T>> k = new List<KeyValuePair<string,T>>();
Dictionary<string, T> d = k.ToDictionary(x => x.Key, x => x.Value);


Thank you.
Regards,
JS-Support

Re: [WORKAROUND] Unimplemented form of generic method parameter: 'T'.

Posted: Mon Mar 05, 2018 12:16 am
by Ajcek
It works. Thanks :)