© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
C#C
C#•3y ago•
9 replies
PatrickG

❔ Cast is not valid when casting interface to comcrete impl

this is my concrete imp:
public class TempFile : IBrowserFile
{
public TempFile(string uuid, string name, long size, string contentType)
{
Uuid = uuid;
Name = name;
Size = size;
ContentType = contentType;
}

public TempFile()
{
}

public string Uuid { get; set; } = "";
public string ContentType { get; set; } = "";
public string Name { get; set; } = "";
public long Size { get; set; }
public DateTimeOffset LastModified => throw new NotImplementedException();

public Stream OpenReadStream(long maxAllowedSize = 512000, CancellationToken cancellationToken = default)
{
throw new NotImplementedException();
}
}



=========================
this is where im trying to cast ibrowserFile as TempFile and getting invalid Cast.

private async Task UploadFileAsync(IBrowserFile file)
{


if (file == null)
return;

if (EditModel == null)
{
throw new Exception("No edit model");
}

if (EditModel.Id == 0)
{
var result = await this.EmployeeComptesDepensesService.UploadTemporaryAttachmentFileAsync(file);
if (!result.ApiCallSuccess)
{
this.serverResponseValidator?.DisplayErrors(result.ServerErrors, result.ApiCallResultErrorMessage);
}
else
{
try
{
TempFile tf = (TempFile)file;
tf.Uuid = result.ResultModel?.Result!;
EditModel.TempFiles.Add(tf);
}
catch (Exception ex)
{

this.serverResponseValidator?.DisplayErrors(null, $"cast error {ex.Message}");
}

}
}



What's going on???
C# banner
C#Join
We are a programming server aimed at coders discussing everything related to C# (CSharp) and .NET.
61,871Members
Resources
Was this page helpful?

Similar Threads

Recent Announcements

Similar Threads

InvalidCastException Specified cast is not valid.
C#CC# / help
4y ago
❔ Defining base impl ctor in interface
C#CC# / help
3y ago
❔ nested generic interface throws cast exception when added to list
C#CC# / help
3y ago
✅ Invalid cast between interface types
C#CC# / help
4y ago