Click here to Skip to main content
15,914,013 members
Home / Discussions / C#
   

C#

 
AnswerRe: ASP.NET c# webservice - Singleton COM instance? Pin
Heath Stewart23-Dec-04 21:13
protectorHeath Stewart23-Dec-04 21:13 
QuestionLocal IP number? Pin
danneau23-Dec-04 19:06
danneau23-Dec-04 19:06 
AnswerRe: Local IP number? Pin
Jay Shankar23-Dec-04 19:55
Jay Shankar23-Dec-04 19:55 
GeneralRe: Local IP number? Thanks! Pin
danneau23-Dec-04 20:43
danneau23-Dec-04 20:43 
GeneralRe: Local IP number? Thanks! Pin
Jay Shankar23-Dec-04 21:03
Jay Shankar23-Dec-04 21:03 
AnswerRe: Local IP number? Pin
leppie23-Dec-04 20:33
leppie23-Dec-04 20:33 
Generalsingleton Pin
lcarriere23-Dec-04 18:14
lcarriere23-Dec-04 18:14 
GeneralRe: singleton Pin
Heath Stewart23-Dec-04 20:20
protectorHeath Stewart23-Dec-04 20:20 
lcarriere wrote:
Is there a way for me to work with this instance in a Type Library using reflection.

Assemblies are not type libraries, which describe COM types. You can't reflect COM types in .NET, though you can reflect their RCWs (runtime callable wrappers), but that's definitely not the same thing. Type metadata and IL modules are stored in assemblies.

If you want to reflect the singleton in such a way that when you access a property then you shouldn't have a problem if you follow the recommend pattern, in which each property gets an instance - which creates the instance if not created already - and returns a value from the instance using the private instance field. The following example also uses the double-checked locking method, which is the safest way of creating a singleton:
using System;
 
class Singleton
{
  static object sync = new object();
  static Singleton instance;
  int serialNumber;
 
  Singleton()
  {
  }
 
  static Singleton Instance
  {
    get
    {
      if (instance == null)
        lock (sync)
          if (instance == null)
            instance = new Singleton();
      return instance;
    }
  }
 
  public static int SerialNumber
  {
    get
    {
      return ++Instance.serialNumber;
    }
  }
}
Notice how the SerialNumber property uses the static private Instance property, which will create a single instance if necessary. When you reflect the static property SerialNumber and instance gets created.

To reflect static members you need to use the overload that allows you to specify a BindingFlags, providing to it at least BindingFlags.Static. The default implementations like GetProperty search for public instance properties, not static and non-public properties. Other than that you would reflect it as you normally would reflect a type with an assembly.

This posting is provided "AS IS" with no warranties, and confers no rights.

Software Design Engineer
Developer Division Sustained Engineering
Microsoft

[My Articles] [My Blog]
GeneralRe: singleton Pin
lcarriere24-Dec-04 4:06
lcarriere24-Dec-04 4:06 
GeneralRe: singleton Pin
Heath Stewart27-Dec-04 10:17
protectorHeath Stewart27-Dec-04 10:17 
GeneralRe: singleton Pin
lcarriere27-Dec-04 11:29
lcarriere27-Dec-04 11:29 
Generalsuspending layout, useragents, and creating shortcuts... Pin
dkarlton23-Dec-04 15:36
dkarlton23-Dec-04 15:36 
GeneralRe: suspending layout, useragents, and creating shortcuts... Pin
Heath Stewart23-Dec-04 20:48
protectorHeath Stewart23-Dec-04 20:48 
GeneralRe: suspending layout, useragents, and creating shortcuts... Pin
dkarlton27-Dec-04 14:32
dkarlton27-Dec-04 14:32 
GeneralRe: suspending layout, useragents, and creating shortcuts... Pin
Heath Stewart27-Dec-04 19:19
protectorHeath Stewart27-Dec-04 19:19 
GeneralRe: suspending layout, useragents, and creating shortcuts... Pin
Heath Stewart28-Dec-04 5:09
protectorHeath Stewart28-Dec-04 5:09 
GeneralRe: suspending layout, useragents, and creating shortcuts... Pin
dkarlton28-Dec-04 8:28
dkarlton28-Dec-04 8:28 
GeneralRe: suspending layout, useragents, and creating shortcuts... Pin
Heath Stewart28-Dec-04 8:43
protectorHeath Stewart28-Dec-04 8:43 
GeneralRe: suspending layout, useragents, and creating shortcuts... Pin
dkarlton14-Jan-05 14:44
dkarlton14-Jan-05 14:44 
GeneralRe: suspending layout, useragents, and creating shortcuts... Pin
dkarlton29-Mar-05 5:14
dkarlton29-Mar-05 5:14 
GeneralNo ToolBarButton images in runtime Pin
Skynyrd23-Dec-04 13:30
Skynyrd23-Dec-04 13:30 
GeneralRe: No ToolBarButton images in runtime Pin
Heath Stewart23-Dec-04 20:49
protectorHeath Stewart23-Dec-04 20:49 
GeneralRe: No ToolBarButton images in runtime Pin
Skynyrd24-Dec-04 0:54
Skynyrd24-Dec-04 0:54 
GeneralSendMessage issue Pin
Aviv Halperin23-Dec-04 10:32
Aviv Halperin23-Dec-04 10:32 
GeneralRe: SendMessage issue Pin
Matt Gerrans23-Dec-04 11:30
Matt Gerrans23-Dec-04 11:30 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.