C# Get name of generic class via `nameof` [duplicate] - c#

This question already has answers here:
C# nameof generic type without specifying type
(3 answers)
Closed 10 days ago.
I have a class:
public class StateMachine<TTag> where TTag : Enum { ... }
Which logs using Log that accepts as first argument string tag. I use nameof with StateMachine<TTag> as an argument:
_logger.Log(nameof(StateMachine<TTag>), ...)
In my app I have instances of StateMachine<TTag>, like StateMachine<AppStateTag>, etc.
And I want my StateMachine to log like this:
StateMachine<AppStateTag> ...
But instead I get:
StateMachine ...
Do I need to get weird like this?
${nameof(StateMachine)}<{nameof(TTag)}>
Is there any simple way of doing this?

So, ok, using answers from here, I've managed to frankenstein this:
$"{nameof(StateMachine<TTag>)}<{typeof(TTag).Name}>"
If anyone can suggest a better way of doing this, it would be very much appreciated.

Related

Is it possible to create indexer in helper class c# [duplicate]

This question already has answers here:
C# extend indexer?
(6 answers)
Closed 1 year ago.
I am intrested in creating custom indexer for char[,]. It seems to me that it is prohibited, but I am wondering if there is an oportunity. The code that could have solved the problem is:
public static class GeneratorHelpers
{
public static char int[Vector2D position] (this char[,] field)
{
return field[position.X, position.Y];
}
}
The above code does not compile.
Currently, there is no such things as extension properties, operators or indexers in the C# language. The most common pattern for this would be just to take additional parameters to an extension method.

How to extend the string class in c# to change its behavior? [duplicate]

This question already has answers here:
Add new property to string class C# [duplicate]
(3 answers)
Closed 7 years ago.
I don't like the way string behaves and there are a few other things I would like to change.
It appears string cannot be extended because it is a sealed class.
Is there another way? I could copy the source code and make my own class but then it wouldn't be compatible with string, or could I make it compatible?
You could use extension methods to extend String. The link below explains extension methods and has an example of how to add a WordCount() function to String.
https://msdn.microsoft.com/en-us/library/bb383977.aspx

Creating an instance from a class defined by a string [duplicate]

This question already has answers here:
Create an instance of a class from a string
(8 answers)
Closed 8 years ago.
In a namespace I have an arbitrary number of classes fulfilling an interface IModel. Given the name of a class as a string, I want to instantiate that class and store the resulting object in a variable of type IModel.
As I have no experience in reflection, I did not figure out how to do it.
You don't need reflection here - use Activator
IModel model = (IModel)Activator.CreateInstance(Type.GetType(typeName));
You could have a look at Activator.CreateInstance.
There are many ways to do this. I do the following:
Type t = Type.GetType("<name of class>");
IModel m = (IModel)Activator.CreateInstance(t); // assuming constructor has no parameters

How to check if a class inherits another class without instantiating it? [duplicate]

This question already has answers here:
How do I check if a type is a subtype OR the type of an object?
(5 answers)
Closed 9 years ago.
Suppose I have a class that looks like this:
class Derived : // some inheritance stuff here
{
}
I want to check something like this in my code:
Derived is SomeType;
But looks like is operator need Derived to be variable of type Dervied, not Derived itself.
I don't want to create an object of type Derived.
How can I make sure Derived inherits SomeType without instantiating it?
P.S. If it helps, I want something like what where keyword does with generics.
EDIT:
Similar to this answer, but it's checking an object. I want to check the class itself.
To check for assignability, you can use the Type.IsAssignableFrom method:
typeof(SomeType).IsAssignableFrom(typeof(Derived))
This will work as you expect for type-equality, inheritance-relationships and interface-implementations but not when you are looking for 'assignability' across explicit / implicit conversion operators.
To check for strict inheritance, you can use Type.IsSubclassOf:
typeof(Derived).IsSubclassOf(typeof(SomeType))
Try this
typeof(IFoo).IsAssignableFrom(typeof(BarClass));
This will tell you whether BarClass(Derived) implements IFoo(SomeType) or not

C#: How to set default values of properties [duplicate]

This question already has answers here:
Closed 12 years ago.
Possible Duplicate:
How do you give a C# Auto-Property a default value?
Hi all:
If I want to add default value, says 60, to following property for a class, what is the most convenient way in C#3.0
public int someSetting { get; set; }
You need to initialize it in all constructors.
Put it in the parameterless constructor, that's what I'd do.
Edit: Yes, I guess what I mean by the above is any constructor where the parameter in question is not passed in.
I would think the best way is to set the default in the constructor of the class

Categories

Resources