This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
C# getting its own class name
For Visual Studio, I want to create a class template and use a string field that holds the name of a class itself. Is it possible?
For example:
public class MyClass
{
public string TAG = GetClassName(this);
}
When talking about non-static methods use Object.GetType Method which returns the exact runtime type of the instance (a reference to an instance of the Type Class):
this.GetType().Name
When talking about static methods, use MethodBase Class and its GetCurrentMethod Method :
Type t = MethodBase.GetCurrentMethod().DeclaringType;
//t.Name
However, see this post on SO for more info on this.
public string GetClassName(object item)
{
return typeof(item).Name;
}
Try following:
this.GetType().Name
Type.Name -> Gets the name of the current member.
Related
This question already has answers here:
Default parameter for value must be a compile time constant?
(7 answers)
Closed 9 months ago.
This post was edited and submitted for review 9 months ago and failed to reopen the post:
Original close reason(s) were not resolved
I'm attempting to build an 'Action' abstract class that will be used in many different ways. An Action, in my case, is just a simple repeatable task that is called by various 'Triggers'. I've also created an 'ActionArgs' class to be used as a structure to hold arguments for the Action to be performed. The default 'ActionArgs' object doesn't contain any arguments, which is intended to act as the default. However, I want to be able to create 'Action's and 'ActionArgs' that inherit from their respective parents, but I always want the base empty ActionArgs object to act as the default value for an optional parameter, as some Actions will have parameters, others might not. I'm having problems achieving this. I've tried a few things, but this is what I'm kind of going for:
public abstract class Action
{
public string Name { get; private set; }
public Action(string name) { Name = name; }
public abstract bool PerformAction(ActionArgs args = ActionArgs.Empty);
}
public class ActionArgs
{
public static readonly ActionArgs Empty = new ActionArgs();
public ActionArgs() { }
}
In attempting this, I get an error on the PerformAction() definition, says that a default parameter value must be a compile-time constant. Why is this impossible to resolve this at compile time? I'm guessing it's because it is assuming the worst that you have some run-time variation in the constructor. In my case I clearly do not, but is there any way that I can approach this in a way that doesn't involve using null as the default parameter?
I would use a method overload:
public abstract class Action
{
public string Name { get; private set; }
public Action(string name) { Name = name; }
public bool PerformAction() => PerformAction(ActionArgs.Empty);
public abstract bool PerformAction(ActionArgs args);
}
This question already has answers here:
How can I find the method that called the current method?
(17 answers)
How do I get the calling method name and type using reflection? [duplicate]
(5 answers)
Get the calling function name from the called function [duplicate]
(2 answers)
How to find the FULL name of the calling method in C#
(5 answers)
Closed 1 year ago.
Here is my problem: I have two classes which each include a method. Method 1 of class 1 calls method 2 of class 2.
Now, I need to store in a local variable of method 2 (of type string probably), the name of the method that called method 2 (i.e. Method1).
For this I have set up a global variable named string str in class 2. However, as we create a new instance of class 2 when we call method 2, we have: str=null.
Here is the code so that you understand the problem:
public class Class1
{
private Class2 _class2 = new Class2();
public Class2 Class2 { get => _class2; set => _class2 = value; }
public void Method1()
{
Class2.data = "Changes I want to Make in Class2";
Class2.Method2();
}
}
public class Class2
{
public string data;
public void Method2()
{
string str = data; //str=null instead of str="Changes I want to Make in Class2"
//Rest of the code here
}
}
I know that str=null since I instantiate the object Class2 = new Class2().
So, I need to find another way around the problem, and I realised that, in my case, getting the name of Method 1 from a local variable in Method 2 would be a solution.
My question is therefore the following: Is there a way to answer this problem?
Note: I am a beginner in computer development, so I apologise in advance if the solution is easy and I have not seen it before.
This question already has answers here:
C# static vs instance methods
(8 answers)
Closed 6 years ago.
Let's say I have a class
public class product
{
public string GetName()
{
return "product";
}
public static string GetStaticName()
{
return "product";
}
}
These methods do the same thing but one is static and one isn't.
When i call these method I do this:
product p = new product();
string _ProductName = p.GetName();
and
string _ProductName = product.GetStaticName();
Which method is the best to use for performance etc?
Which method is the best to use for performance etc?
You haven't to address any performance issue here. You just have to decide if this method should be the same for all the instances of the objects you create. If the answer is yes, then it should be a static method. Otherwise, it should be an instance method. It's a design decision, not a performance decision.
By the way, based on your code. I don't think that in this case you have to address even that. You just want to get the name of the product and probably set it. That being said the following is the only thing you need.
public class Product
{
public string Name { get; set; }
}
This question already has answers here:
"Inconsistent accessibility" on class definition
(3 answers)
Closed 7 years ago.
I do not know what to do with this problem
He does not accept " public " in enum FormDataEntryFilterType and I want it to change it from the outside
If anyone can help me, thanks a lot
the code :
enum FormDataEntryFilterType
{
integerNumber,
DecimalNumber,
String
}
public partial class FormDataEntry : Form
{
public static string InputResult;
**public** FormDataEntryFilterType type = FormDataEntryFilterType.DecimalNumber;
.
.
.
Your FormDataEntry.type field is public, but its enum type is private. Your enum defaults to private because no access modifier has been specified.
To fix this, you can make your enum public:
public enum FormDataEntryFilterType
{
integerNumber,
DecimalNumber,
String
}
FormDataEntryFilterType is private to the assembly. You cannot then return its value from a public method, because the caller does not have access to the enum.
Change the enum to public
public enum FormDataEntryFilterType
{
...
}
This question already has answers here:
How to get the type of T from a member of a generic class or method
(17 answers)
Closed 9 years ago.
I'm working on a project where I have to reflect through data models to find out what type is in each property on a data models. I have the code working for all cases except for generic collections. I have to be able to what T is in IList.
I have the following data model
public class ArrryOfObjects
{
public NestModelNestedClass NestClass { get; set; }
public int IntObject { get; set; }
public IList<NestModelNestedClass> ListOfObjects { get; set; }
}
I've seen a couple of examples, like https://stackoverflow.com/a/1043778/136717 on how to do this, but they use the type.GetGenericTypeDefinition() to be get the type. But in my sample I cannot use this because 'type.IsGeneric.Parameter' is false.
I've review Type documentation and don't understand how to do this.
Try this:
var t = typeof(ArrryOfObjects)
.GetProperty("ListOfObjects")
.PropertyType
.GetGenericArguments()[0];
This is how it works:
From the type of ArrryOfObjects...
obtain the property called ListOfObjects...
get the type of that property...
which we know to be a generic type with at least one type parameter.
We get the first type parameter - in your example it should be typeof(NestModelNestedClass)
P.S. GetGenericTypeDefinition gets you typeof(IList<>), the generic type of which IList<NestModelNestedClass> is a generic instance.