User Tools

Site Tools


dev:csharp

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Next revision
Previous revision
dev:csharp [2023/03/23 15:09] – created adamdev:csharp [2024/12/08 03:53] (current) – [entityframework (my beloved)] adam
Line 15: Line 15:
 </code> </code>
  
-so you make an abstract class `MyAbstractClassand derive from it. +so you make an abstract class ''MyAbstractClass'' and derive from it. 
  
  
Line 62: Line 62:
  
 observe - I write a text string in a config file to describ ea type, and now I can pick what object to instantiate :))) observe - I write a text string in a config file to describ ea type, and now I can pick what object to instantiate :)))
 +
 +======= nuget on non-windows =======
 +
 +this comes up a lot.
 +
 +> it's easy just install it via nuget with <code>Install-Package WebDav.Client</code>
 +
 +- normal devs
 +
 +for some reason, it works differently when you're not on windows. what we do:
 +
 +<code>dotnet add package WebDav.Client</code>
 +
 +why is not ''dotnet nuget Install-Package WebDav.Client''? why does //the quintessential nuget function// not involve the word "nuget"? All I got for you is a nice big shrug.
 +
 +======= entityframework (my beloved) =======
 +
 +install in your dev env:
 +
 +  dotnet tool install --global dotnet-ef
 +
 +install packages:
 +  * Microsoft.EntityFrameworkCore.Design
 +  * Npgsql.EntityFrameworkCore.PostgreSQL (or whatever you like)
 +
 +  dotnet-ef dbcontext scaffold "Host=HOST;Database=DBNAME;Username=USERNAMEPassword=PASSWORD;IncludeErrorDetail=true;" Npgsql.EntityFrameworkCore.PostgreSQL
 +
 +
 +migrations:
 +  dotnet ef migrations add "name of migration"
 +  dotnet ef database update --connection "Host=asdfasdfasdf etc"
 +
 +======= enum names =======
 +
 +forget "switch(enum) return string" functions, do it with decorators! https://stackoverflow.com/questions/479410/enum-tostring-with-user-friendly-strings
 +
 +make yourself this function:
 +<code csharp>
 +public static string GetDescription<T>(this T enumerationValue)
 +    where T : struct
 +{
 +    Type type = enumerationValue.GetType();
 +    if (!type.IsEnum)
 +    {
 +        throw new ArgumentException("EnumerationValue must be of Enum type", "enumerationValue");
 +    }
 +
 +    //Tries to find a DescriptionAttribute for a potential friendly name
 +    //for the enum
 +    MemberInfo[] memberInfo = type.GetMember(enumerationValue.ToString());
 +    if (memberInfo != null && memberInfo.Length > 0)
 +    {
 +        object[] attrs = memberInfo[0].GetCustomAttributes(typeof(DescriptionAttribute), false);
 +
 +        if (attrs != null && attrs.Length > 0)
 +        {
 +            //Pull out the description value
 +            return ((DescriptionAttribute)attrs[0]).Description;
 +        }
 +    }
 +    //If we have no description attribute, just return the ToString of the enum
 +    return enumerationValue.ToString();
 +}
 +</code>
 +
 +and then all you have to do is use ''Description'' from ''System.ComponentModel'':
 +<code csharp>
 +private enum PublishStatusValue
 +{
 +    [Description("Not Completed")]
 +    NotCompleted,
 +    Completed,
 +    Error
 +};
 +</code>
 +
 +======= what is the actual publish command =======
 +
 +specify output folder. I don't know what all the other bullshit it insists on dumping in the folder is.
 +
 +<code>
 +dotnet publish -c Release -o ../publish
 +</code>
 +
 +also you can't not get ''appsettings.Development.json'' to show up in release. your only option is to delete it manually.
 +======= why is asp.net not accepting my frombody thing? =======
 +
 +microsoft replaced the working json parser with one that doesn't. https://devblogs.microsoft.com/dotnet/try-the-new-system-text-json-apis/
 +
 +fix: 
 +  - ''dotnet add package Microsoft.AspNetCore.Mvc.NewtonsoftJson --version [compatible version]''
 +  - go find where services are being added; ''services.AddControllers().AddNewtonsoftJson();''. (hint: Builder.Services?)
 +
 +======= which is a field, which is a property? =======
 +
 +<code csharp>
 +public class MyClass
 +{
 +    // this is a field.  It is private to your class and stores the actual data.
 +    private string _myField;
 +
 +    // this is a property. When accessed it uses the underlying field,
 +    // but only exposes the contract, which will not be affected by the underlying field
 +    public string MyProperty
 +    {
 +        get
 +        {
 +            return _myField;
 +        }
 +        set
 +        {
 +            _myField = value;
 +        }
 +    }
 +
 +    // This is an AutoProperty (C# 3.0 and higher) - which is a shorthand syntax
 +    // used to generate a private field for you
 +    public int AnotherProperty { get; set; } 
 +}
 +</code>
 +
 +src: https://stackoverflow.com/a/295109/1173856
dev/csharp.1679584183.txt.gz · Last modified: 2023/03/23 15:09 by adam