Click here to Skip to main content
15,173,299 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello,

I have write this code but its getting error:-
acceptedCommandsFieldInfo is a field but is used like a type


Pleas help me how to resolve this error.

Thanks in Advance.

Ankit Agarwal
Software Engineer

What I have tried:

class MyService
    {
        const int SERVICE_ACCEPT_PRESHUTDOWN = 0x100;
        const int SERVICE_CONTROL_PRESHUTDOWN = 0xf;
        FieldInfo acceptedCommandsFieldInfo =
        typeof(ServiceBase).GetField("acceptedCommands", BindingFlags.Instance | BindingFlags.NonPublic);
        if (acceptedCommandsFieldInfo == null)// Error in this line
            throw ApplicationException("acceptedCommands field not found");// Error in this line
    
            int value = (int)acceptedCommandsFieldInfo.GetValue(this);// Error in this line
            acceptedCommandsFieldInfo.SetValue(this, value SERVICE_ACCEPT_PRESHUTDOWN);// Error in this line
    }
Posted
Updated 1-Jan-17 21:41pm
v2

1 solution

The problem is that you are running this code right in the class. You cannot do this, you have to run the code in a method in your class.

It looks like you want to create a service? Then put it in an OnStart function (but then your class has to derive from ServiceBase, too). Also in your last line, you pass "value SERVICE_ACCEPT_PRESHUTDOWN" which look like two variables. You can only pass one. I don't know exactly what you try to do, so you have to know yourself which one to pass there (but I guess it will be SERVICE_ACCEPT_PRESHUTDOWN). Also, you have to use throw new ApplicationException instead of throw ApplicationException.
C#
class MyService : ServiceBase
{
    protected override void OnStart(string[] args)
    {
            FieldInfo acceptedCommandsFieldInfo = typeof(ServiceBase).GetField("acceptedCommands", BindingFlags.Instance | BindingFlags.NonPublic);
            if (acceptedCommandsFieldInfo == null)
                throw new ApplicationException("acceptedCommands field not found");
    
            int value = (int)acceptedCommandsFieldInfo.GetValue(this);
            acceptedCommandsFieldInfo.SetValue(this, SERVICE_ACCEPT_PRESHUTDOWN);
    }
}
   
v2
Comments
Agarwal1984 2-Jan-17 3:56am
   
Now error is coming in this line:-
ApplicationException

It is 'type' but is used like a 'variable'
Thomas Daniels 2-Jan-17 3:59am
   
Fixed.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900