This question already has answers here:
Get only Methods with specific signature out of Type.GetMethods()
(5 answers)
Closed 8 years ago.
In C++, we can scan through the memory to get the function by some assembly pattern, I'm thinking about that if we can have a relevant ways to get the function by using function signature in .Net, and if could, post an example.
You are looking for this overloaded method:
public MethodInfo GetMethod(
string name,
Type[] types
)
Searches for the specified public method whose parameters match the specified argument types.
You could find examples in the documentation.
Related
This question already has answers here:
Get the type name
(10 answers)
Closed 2 years ago.
I have an enumerable as follows:
IEnumerable<SomeObjectType> dataToImport;
At runtime I run the following code:
dataToImport.GetType().ToString()
So far so good. Checking results at runtime shows me something like the following :
System.Collections.Generic.List`1[SomeObjectType]
Can somebody tell me what that `1 means and where it is coming from? Should I expect this on all collections?
It's a placeholder for the first generic type argument. You should expect it on anything with a generic type.
The backtick(`) followed by a digit represent the number of generic arguments. For example List<T> has one generic argument hence `1
This question already has answers here:
How do I use reflection to call a generic method?
(8 answers)
Generic Method Executed with a runtime type [duplicate]
(5 answers)
Closed 8 years ago.
I want to be able to take in a string and call GetType() and find the type. Which I do here and it works perfectly fine.
Type TypeToUse = typeof(someclass).Assembly.GetType("MyProj.Data.Stuff.MyClass");
But when I use the type I found TypeToUse to create my observable collection it says the namespace can't be found.
ObservableCollection<TypeToUse> MyList = new ObservableCollection<TypeToUse>();
How do I fix this?
I'm actually trying to do this with a generic repository I'm assuming I have to add public virtual Type MakeGenericType(params Type[] typeArguments); to the Repository class?
Type elementType = typeof(ColorFilter).Assembly.
GetType("Photometrics.Data.Model.Entity.PhantomModels.Chamber");
Type CoreRepoType = typeof(CoreRepository<>).Assembly.
GetType("Photometrics.Data.Sql.Repository.CoreRepository");
Type combinedType = CoreRepoType.MakeGenericType(elementType);
This question already has answers here:
How do I use reflection to call a generic method?
(8 answers)
Closed 9 years ago.
The generic Method is...
public void PrintGeneric2<T>(T test) where T : ITest
{
Console.WriteLine("Generic : " + test.myvar);
}
I'm calling this from Main()...
Type t = test2.GetType();
PrintGeneric2<t>(test2);
I get error "CS0246: the type or namespace name 't' could not be found" and "CS1502: best overloaded method match DoSomethingClass.PrintGeneric2< t >(T) has invalid arguments"
this is related to my previous question here: C# : Passing a Generic Object
I've read that the generic type can't be determined at runtime, without the use of reflection or methodinfo, but I'm not very clear on how to do so in this instance.
Thanks if you can enlighten me =)
If you really want to invoke a generic method using a type parameter not known at compile-time, you can write something like:
typeof(YourType)
.GetMethod("PrintGeneric2")
.MakeGenericMethod(t)
.Invoke(instance, new object[] { test2 } );
However, as stated by other responses, Generics might not be the best solution in your case.
Generics offer Compile Time parametric polymorphism. You are trying to use them with a type specified only at Runtime.
Short answer : it won't work and it has no reason to (except with reflection but that is a different beast altogether).
Just call:
PrintGeneric2(test2);
The compiler will infer <t> from what you pass.
This question already has answers here:
Is it possible to get parameters' values for each frame in call stack in .NET
(2 answers)
Closed 10 years ago.
From within a method call I need to "jump" three layers up the stack and retrieve the type and value of the parameters passed to that method.
Getting the parameter type is easy but I couldn't find a way to get the value passed to a certain method on the stack.
var st = new StackTrace();
var frames = st.GetFrames();
var methodParameters = frame[2].GetMethod().GetParameters;
// get each parameter value
Note: using StackTraceis not mandatory.
Is there a wayto find a value of a parameter passed to a method during runtime?
I do not think there is a method unless you develop your own system for storing the values.
The reflection namespace represents static data about an assembly, and you would need to retrieve values at runtime.
I found PostSharp mentioned in MSDN forums, but I have never tried it.
This question already has answers here:
How can I get the name of a variable passed into a function?
(23 answers)
Closed 9 years ago.
Duplicate:
Determine the name of the variable used as a parameter to a method
Is there any way to retrieve the name of a parameter that was passed into a method e.g.
int someParameter = 1;
Method(someParameter);
public void Method(int parameter)
{
// I want the name of 'parameter' which will be 'someParameter'.
}
No. It's only the value which is passed in as the argument. All the method gets is the integer. The fact that the expression happened to be just evaluating a variable is unknown as far as your method is concerned.
No, there is no way.
This will be followed by a bunch of people showing weird lambda expression ways to change the call site and kinda get the name, but the short answer is no.
The only way perhaps will be with annotations with run-time retention. But then, it will be the name of the annotation, not of the parameter itself. A parameter name is just a syntactic artifact of the language. It does not get carried over to the compiled result.