Binto86
Binto86
CC#
Created by Binto86 on 11/25/2023 in #help
✅ Is there any speed comparison between running your code normally and running it AOT?
Couldn't find any online, thats why im asking
4 replies
CC#
Created by Binto86 on 8/28/2023 in #help
✅ Can't have interface with abstract static method as parameter in blazor component
I have interface and class like this:
interface IFoo
{
static abstract IFoo Create();
void SomeFunction();
}

class Foo : IFoo
{
public static IFoo Create() => new Foo();
public void SomeFunction() { }
}
interface IFoo
{
static abstract IFoo Create();
void SomeFunction();
}

class Foo : IFoo
{
public static IFoo Create() => new Foo();
public void SomeFunction() { }
}
now i want to have IFoo as parameter in blazor component like this:
[Parameter]
public IFoo MyFoo { get; set; }
[Parameter]
public IFoo MyFoo { get; set; }
but when i try to pass instance of Foo into the component i get this error: CS8920 The interface 'IFoo' cannot be used as type argument. Static member 'IFoo.Create()' does not have a most specific implementation in the interface. Any way to fix this?
7 replies
CC#
Created by Binto86 on 7/1/2023 in #help
✅ Validate input in list in blazor forms
I have blazor EditForm with some inputs, the user can enter multiple people into the form, i handle that by having a button that adds some new fields in which the user can fill out new person. The bind-value for those inputs is the certain index list of people i have inside the main class for model (for example modelClass.People[5]). Now i want to add input validation for example for email, but it doesn't get validated for the inputs that write inside the list. Anyone any idea how to make it work? this is the relevant part of my code:
public class Person
{
[Required]
public string FullName { get; set; }

[Required]
[EmailAddress]
public string Email { get; set; }
}

public class Company
{
[Required]
public string Name{ get; set; }

public List<Person> People = new List<Person>();
}
public class Person
{
[Required]
public string FullName { get; set; }

[Required]
[EmailAddress]
public string Email { get; set; }
}

public class Company
{
[Required]
public string Name{ get; set; }

public List<Person> People = new List<Person>();
}
<EditForm class="form" Model="reg" OnValidSubmit="Submit">
<DataAnnotationsValidator />
<ValidationSummary />

<label>Name of the company:</label> <InputText class="field" @bind-Value="reg.Firm"> <br />
@for (int i = 0; i < people; i++)
{
<h4>@(i + 1). Person</h4>
@AddPersonIfNeeded();
<div class="form">
<label>Fullname:</label> <InputText class="field" @bind-Value="reg.People[0].FullName" /> <br />
<label>Email:</label> <InputText class="field" @bind-Value="reg.People[0].Email"/> <br />
</div>
}
<button type="button" class="button" onclick="@(() => { people++; })">Add new person</button>
<button type="submit" class="button">Submit</button>
</EditForm>
<EditForm class="form" Model="reg" OnValidSubmit="Submit">
<DataAnnotationsValidator />
<ValidationSummary />

<label>Name of the company:</label> <InputText class="field" @bind-Value="reg.Firm"> <br />
@for (int i = 0; i < people; i++)
{
<h4>@(i + 1). Person</h4>
@AddPersonIfNeeded();
<div class="form">
<label>Fullname:</label> <InputText class="field" @bind-Value="reg.People[0].FullName" /> <br />
<label>Email:</label> <InputText class="field" @bind-Value="reg.People[0].Email"/> <br />
</div>
}
<button type="button" class="button" onclick="@(() => { people++; })">Add new person</button>
<button type="submit" class="button">Submit</button>
</EditForm>
3 replies
CC#
Created by Binto86 on 2/16/2023 in #help
❔ Bitmap created from graphics appears to be all black
I have some kind of drawing board in winforms. User draws into panel with graphics and than i want to save the image into file. relevant parts of code are (canvas is the name of the panel the user is drawing on):
Graphics graphics;
Pen pen;
Bitmap bmp;

public Form1()
{
InitializeComponent();
graphics = canvas.CreateGraphics();
pen = new Pen(Color.Black, 35);
bmp = new Bitmap(28, 28, graphics);
}

private void btnSave_Click(object sender, EventArgs e)
{
var fileName = "digit.png";
bmp.Save(fileName,ImageFormat.Png);
}
Graphics graphics;
Pen pen;
Bitmap bmp;

public Form1()
{
InitializeComponent();
graphics = canvas.CreateGraphics();
pen = new Pen(Color.Black, 35);
bmp = new Bitmap(28, 28, graphics);
}

private void btnSave_Click(object sender, EventArgs e)
{
var fileName = "digit.png";
bmp.Save(fileName,ImageFormat.Png);
}
but the saved image is all black, any ideas why?
2 replies
CC#
Created by Binto86 on 2/15/2023 in #help
✅ Can i put constraint on ctor in interface
I know the name is kinda misleading, but i couldn't think of anything else. So can i have interface, that defines how will ctor of the class that implements the interface look like?
4 replies
CC#
Created by Binto86 on 2/10/2023 in #help
✅ ✅ Use function that has default implementation in interface from instance of class
I have interface similar to this:
interface IFoo
{
void DoThing1();
void DoThing2();

void DoMultipleThings(int thing1, int thing2)
{
for (int i = 0; i < thing1; i++)
{
this.DoThing1();
for (int j = 0; j < thing2; j++)
{
this.DoThing2();
}
}
}
}
interface IFoo
{
void DoThing1();
void DoThing2();

void DoMultipleThings(int thing1, int thing2)
{
for (int i = 0; i < thing1; i++)
{
this.DoThing1();
for (int j = 0; j < thing2; j++)
{
this.DoThing2();
}
}
}
}
now i have class like this:
class Bar : IFoo
{
void DoThing1() => //something
void DoThing2() => //something
}
class Bar : IFoo
{
void DoThing1() => //something
void DoThing2() => //something
}
and i want to use it like this:
Bar bar = new Bar();
bar.DoMultipleThings(5, 5);
Bar bar = new Bar();
bar.DoMultipleThings(5, 5);
is this possible? maybe some keyword on the interface or something (i know i can cast it, but i don't want to do that)
9 replies
CC#
Created by Binto86 on 2/2/2023 in #help
✅ Image from dataurl
I have dataurl with image and i need to get it to some reasonable image object in c#, so i can resize it and get bytes from the resized image. I was playing with skiasharp bitmap, but i couldn't figure it out.
45 replies
CC#
Created by Binto86 on 1/7/2023 in #help
✅ Get instance name inside of class
So i have class like this:
private class CliFlag
{
public object Value { get; }
public CliFlag(object value)
{
this.Value = value;
}
}
private class CliFlag
{
public object Value { get; }
public CliFlag(object value)
{
this.Value = value;
}
}
and i want to create ToString that will return -[nameOfTheCliFlag] [valueOfTheFlag] like this
var flag = new CliFlag("x");
flag.ToString(); // -flag x
var flag = new CliFlag("x");
flag.ToString(); // -flag x
however i can't find o way to get the name of the current instance inside of the class, i thought about nameof(this), but that doesn't work is there any way how to get this working without passing the name of the flag from outside?
7 replies
CC#
Created by Binto86 on 9/25/2022 in #help
How to deal with stack overflow error [Answered]
So when doing some programming competitions, i often get stack overflow, when doing recursion and the only way (i know about) to not get stack overflow is to not do deep recursion, but i was wondering, if there is any other way, as i kinda need recursion this time.
36 replies
CC#
Created by Binto86 on 9/5/2022 in #help
Could not load file or assembly 'System.Collections.Imutable' when using source generators
I am trying source generators, and now i am getting warning
Generator failed to generate source. It will not contribute to the output and compilation errors may occur as a result. Exception was of type 'FileNotFoundException' with message 'Could not load file or assembly 'System.Collections.Immutable, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' or one of its dependencies.
Generator failed to generate source. It will not contribute to the output and compilation errors may occur as a result. Exception was of type 'FileNotFoundException' with message 'Could not load file or assembly 'System.Collections.Immutable, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' or one of its dependencies.
i am pretty sure that this error has something to do with context.AdditionalFiles as it didn't occur when i didn't use them, this is my code:
public void Execute(GeneratorExecutionContext context)
{
var mainMethod = context.Compilation.GetEntryPoint(context.CancellationToken);
for (int i = 0; i < context.AdditionalFiles.Length; i++)
{
var file = context.AdditionalFiles[i];
if (file.Path.EndsWith(".py"))
{
var ClassName = Path.GetFileNameWithoutExtension(file.Path);
string source = $@"
// <auto-generated/>
{PythonGenerator.GenerateClass(file.Path, mainMethod.ContainingNamespace.Name)}
";


// Add the source code to the compilation
context.AddSource($"{ClassName}.g.cs", source);
}
}
}
public void Execute(GeneratorExecutionContext context)
{
var mainMethod = context.Compilation.GetEntryPoint(context.CancellationToken);
for (int i = 0; i < context.AdditionalFiles.Length; i++)
{
var file = context.AdditionalFiles[i];
if (file.Path.EndsWith(".py"))
{
var ClassName = Path.GetFileNameWithoutExtension(file.Path);
string source = $@"
// <auto-generated/>
{PythonGenerator.GenerateClass(file.Path, mainMethod.ContainingNamespace.Name)}
";


// Add the source code to the compilation
context.AddSource($"{ClassName}.g.cs", source);
}
}
}
6 replies
CC#
Created by Binto86 on 9/5/2022 in #help
Visual Studio doesn't work as expected with source generators [Answered]
20 replies
CC#
Created by Binto86 on 8/30/2022 in #help
Post build event error [Answered]
this is not really c# question, but i am trying to setup postbuild event with my project rn it only echos the project folder. it works fine until i add if $(ConfigurationName) == Debug my working thing: echo $(ProjectDir)bin\Release non working:
if $(ConfigurationName) == Debug
(echo $(ProjectDir)bin\Release)
if $(ConfigurationName) == Debug
(echo $(ProjectDir)bin\Release)
error: Error MSB3073 The command "if Debug == Debug
5 replies
CC#
Created by Binto86 on 8/21/2022 in #help
Generate markdown docs [Answered]
Can you please recommend me some some documentation generator, that is easy to use, ideally bit customizable? Thanks
4 replies
CC#
Created by Binto86 on 8/18/2022 in #help
set default value of parameter to other parameter [Answered]
ok, i am aware that this is probably not possible, but can i set default value of parameter x to value of parameter y? consider this example:
public void MyMethod(string x, string y=x)
{
//some code
}
public void MyMethod(string x, string y=x)
{
//some code
}
would this be possible?
16 replies
CC#
Created by Binto86 on 8/11/2022 in #help
How to add command to right click context menu for solution explorer in VS 2022 extension
I am trying to create .vsix extension , i want to create command which shows up when i right click .razor file, but i wasn't able to find the correct id for parent to do this, can someone help me please?
48 replies