UnemployedNinja
✅ Default interface methods - Am I using them wrong?
I'm trying to learn when/where to properly use interfaces, and I don't understand why my default method doesn't work.
I have this:
Creating a new instance of
Employee
should give me access to employee.Foo()
, but it doesn't.
Maybe I'm just overlooking something simple, but I don't understand :/10 replies
Best way to deserialize duplicate JSON API responses (Using Newtonsoft.JSON)
I'm using a HTTP client to contact an API, and then deserializing the JSON response into an instance of its respective object type.
My issue is this - the API has two versions:
- Version 1: The old API, but still active, however, it's not maintained and may be missing bits of data that are present in requests made to v2 of the API.
- Version 2: The new API. Almost identical responses to v1, but all of the fields are obfuscated.
So an API response for version 1 and 2 may look like this:
Version 1:
Version 2:
If I were to to create a class for this response, it may look like this:
As you can see, this makes it difficult to reuse the same class type for v2 data. Is there a "clean" way I to achieve using only on class type for deserialization of both API versions?
73 replies
✅ Is this necessary?
If
using
disposes of everything inside of it, why is this still a thing?
Wont HttpResponseMessage
be disposed of once the client is finished anyway? o.O
Just wanting to understand it better, idk what other channels to use7 replies
Thread.sleep in a constructor
Since you can't await in a constructor, I'm calling an async method and just sleeping the thread for 10ms while the task is incomplete:
Is this ok, or is there a better way to do this?
... or should I just be designing my code better in the first place? 😬
14 replies
Specify timeout for websocket connection
I'm using Marfusios' websocket-client and I'm trying to specify a timeout period for establishing the connection, but I'm unsure how, or if it's even possible.
The GitHub demonstrates creating a factory for some client settings, but there are no options for specifying a timeout period
One client constructor gives the option for a
connectionFactory
, where a cancelation token can be provided, but I'm unure how to use it (I'm still pretty fresh to C#):
5 replies
Struggling with generic class... design?
I have
BaseClass
that other classes will extend from:
The extending classes will have properties that BaseClass
doesn't have, do knowing the type is important for deserialization.
Instead of using MyClassA.JsonDeserialiseAs<MyClassA>(json)
every time, I'd like to be able to use MyClassA.JsonDeserialize(json)
What's the best way I can achieve this?4 replies
Check value type of extended class instance
I have
BaseClass
and MyClass : BaseClass
.
Using val is BaseClass
will return true for any object that is, or extends BaseClass
.
How can I check if the object is of type MyClass
, and not BaseClass
?13 replies
Custom Newtonsoft JSON deserialization
I have a dictionary where:
- The key will always be a string
- The value will be either:
- string
- an object of
MyClass
Normally, you would do something like:
However, this wont deserialize the non-string value into an instance of MyClass
like it normally would.
How can tell the deserializer how to deserialize a string vs an object?21 replies
Different HttpClient headers
When I make an HTTP request in my browser (Chrome), I get different response headers than I do in my program using
HttpClient
with a HttpClientHandler
I'm using all the exact same headers, including the same cookies, and making the request to the same URL - I also have redirects disabled
Why could this be?
Chrome shows me 11 headers, whereas C#'s HttpClient
only shows me 6 headers from the exact same request17 replies
Dynamically assign values to class instance properties / Indexing class instance properties
I have a class like:
Assuming I have a dictionary like:
Is it possible to do something like my method
FromData
?
Where if it exists, each property will be assigned the value from the dictionary?15 replies
Publishing/Building project with dependencies based on target .NET version
I have a project that requires an external library file, which I have different versions of, for different .NET versions. How can I tell Visual Studio to build my project with the right file?
3 replies
Packing an external DLL with my own DLL
I've written a library that also uses another external library, packed into a .dll file. When I build my library file, it doesn't pack the external dll in with it. How can I do this? Do I need the source?
29 replies
Reduce File Size
I have a very simple console application written in C#, barely more than a couple megabytes. When I publish my project to a self-contained single-file executable, the file comes out to about 50mb.
What's the easiest way to remove unused dependencies and only publish with what it needs?
22 replies
Check if an object is a Dictionary of any type
I'm trying to type-check a method parameter using
if (dict.GetType() == typeof(Dictionary))
.
How can I check for all types of Dictionary
without having to write them all out.
Or am I wasting my time here, since the language has type-checking anyway?30 replies