Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

'System.DateTime' is not a valid Windows Runtime parameter type

I'm using a C# class and it works perfectly fine in my Windows Store App (C#). But when I try to use it in a Windows Runtime Compenent I get the following error:

Calculator.Calculate(System.DateTime)' has parameter 'dateTime' of type 'System.DateTime'. 'System.DateTime' is not a valid Windows Runtime parameter type.

A sample object in the class:

public DateTime Calculate(DateTime dateTime)
{
   int dayNumberOfDateTime = ExtractDayNumber(dateTime);
   int sunRiseInMinutes = CalculateSunRiseInternal(tanSunPosition, differenceSunAndLocalTime);
   return CreateDateTime(dateTime, sunRiseInMinutes);
}

How can I fix this? And what is the problem?

like image 214
Niels Avatar asked Dec 08 '12 15:12

Niels


2 Answers

When you create a Windows Runtime Component then your component can be used by languages that are not managed, like Javascript or C++. Clearly those languages have no idea how to generate a proper System.DateTime, it is a specific .NET type.

Such components must therefore only use native WinRT types and otherwise observe the restrictions present in WinRT. One such restriction you'll run into from the get-go is that WinRT does not support implementation inheritance. Which requires you to declare your class sealed.

The native WinRT types are very unlike the .NET types. The real runtime type that can store a date is Windows.Foundation.DateTime. A string is actually an HSTRING handle. A List is actually an IVector. Etcetera.

Needless to say, if you would actually have to use those native types then your program wouldn't resemble a .NET program anymore. And you don't, the .NET 4.5 version of the CLR has a language projection built in. Code that automatically translates WinRT types to their equivalent .NET types. That translation has a few rough edges, some types cannot easily be substituted. But the vast majority of them map without trouble.

System.DateTime is one such rough edge. The language projection of Windows.Foundation.DateTime is System.DateTimeOffset. So simply solve your problem by declaring your method like this:

public DateTimeOffset Calculate(DateTimeOffset dateTime) {
    // etc..
}

The only other point worth noting is that this is only required for members that other code might use. Public members.

like image 111
Hans Passant Avatar answered Sep 24 '22 01:09

Hans Passant


I was also facing the same issue in my Windows Runtime Component as below.

Severity Code Description Project File Line Suppression State
Error Method '.put_TxPower(System.SByte)' has parameter 'value' of type 'System.SByte'.  
System.SByte' is not a valid Windows Runtime parameter type.

As Lukasz Madon mentioned in the comment, changing the access modifier from the public to internal worked for me.

Before:

public sbyte TxPower { get; set; }

After:

internal sbyte TxPower { get; set; }
like image 22
Sibeesh Venu Avatar answered Sep 21 '22 01:09

Sibeesh Venu