Jump to content


 


Register a free account to unlock additional features at BleepingComputer.com
Welcome to BleepingComputer, a free community where people like yourself come together to discuss and learn how to use their computers. Using the site is easy and fun. As a guest, you can browse and view the various discussions in the forums, but can not create a new topic or reply to an existing one unless you are logged in. Other benefits of registering an account are subscribing to topics and forums, creating a blog, and having no ads shown anywhere on the site.


Click here to Register a free account now! or read our Welcome Guide to learn how to use this site.

Photo

How to invoke Static Net Framework methods for a class names in variables?


  • Please log in to reply
2 replies to this topic

#1 SYOB SYOT

SYOB SYOT

  • Members
  • 16 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:YUGOSLAVIA
  • Local time:05:36 AM

Posted 25 February 2015 - 12:22 PM

HAY PEOPLE,

I AM TRYING TO DO SOMETHING, BUT WITHOUT SUCCESS.

I WANT TO KNOW, HOW CAN I INVOKE SOME STATIC METHODS FROM NET FRAMEWORK CLASS, IF THE NAME OF THE CLASS IS SAVED IN A VARIABLE?

SO, THIS IS THE PROBLEM, FOR EXAMPLE:

IF I USE:

[NAMESPACE.CLASS]::STATICMETHOD()

IT WORKS GREAT.

BUT IF I SAVE THE NAME OF THE CLASS IN THE VARIABLE LIKE THIS:

$VARIABLE=NAMESPACE.CLASS
[$VARIABLE]::STATICMETHOD()

IT DOES NOT WORK.

AND I NEED TO CALL METHODS FROM THE NAME IN VARIABLE.

DO YOU KNOW HOW TO MAKE IT INTERPRET VARIABLE AS CLASS NAME?

 

BY THE WAY, THE PROGRAMMING LANGUAGE IS POWER SHELL 4, AND OPERATING SYSTEM IS WINDOWS 6.2.



BC AdBot (Login to Remove)

 


#2 Billy O'Neal

Billy O'Neal

    Visual C++ STL Maintainer


  • Malware Response Team
  • 12,304 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:Redmond, Washington
  • Local time:09:36 PM

Posted 25 February 2015 - 12:40 PM

Can you not use all caps please?

Generally speaking you can't just call a method based on a class name in a variable -- C# (and VB.NET) are statically compiled languages where the methods are bound at compile time, not run time. Even with dynamic you can only dynamicly bind to instance methods, not static methods.

You'll have to dig around in System.Reflection to get the MethodInfo describing the method you want to call, and call MethodInfo.Invoke. Note that this is strongly not recommended for production code because it makes your program brittle (things can't be renamed / moved around because you don't find out if you broke things until run time).

Billy3

Edited by Billy O'Neal, 25 February 2015 - 12:40 PM.

Twitter - My statements do not establish the official position of Microsoft Corporation, and are my own personal opinion. (But you already knew that, right?)
Posted Image

#3 SYOB SYOT

SYOB SYOT
  • Topic Starter

  • Members
  • 16 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:YUGOSLAVIA
  • Local time:05:36 AM

Posted 25 February 2015 - 02:58 PM

NO.

OKEY, THANK YOU. :)






0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users