Check where library is called from

  • Thread starter Thread starter Nikolay Petrov
  • Start date Start date
N

Nikolay Petrov

I have an class library, which I shared with some of my apps.
Some of them are Winfroms apps, others are ASP .NET.
How could I check from a method in my library, is it called form an
Winforms or ASP .NET?

tnx
 
You could use reflection to walk the stack and see who called you, but... If
you have a routine that wants to behave differently based on who calls it,
you would really be better off providing an argument to that routine to
specify the desired behavior explicitly. Along the same lines, you could
give the class a parameterized constructor that lets the instance know
whether it has been instantiated by WinForms or ASP.NET; store that info in
a class variable, and let the instance methods use that info as they see
fit.
 
I don't realy want to do it this way.
The library hold 100-120 classes and modifing their constructors is a
big pain. That is way I asked the question.
I need this only in method level.
 
Nikolay,
I would consider having a value in my app.config/web.config that indicated
if the app itself is a web app or a win forms app. Especially if web or win
determined how a specific routine behaved. The specific routine would be an
overridable method of an abstract base class (a MustInherit class), where
the app.config indicated the specific concrete class to create (basically a
Strategy pattern & Plug-in pattern
http://www.martinfowler.com/eaaCatalog/plugin.html)

My app/dll would only use the StrategyBase abstraction, while there would be
specific implementations for both ASP.NET & Win Forms. This would also allow
defining a Console App & Windows Service strategies also.

I would store the full name of the concrete class that inherits from the
abstract base class in the app.config file. I then use
System.Activator.CreateInstance to create instances of this class. Something
like:

<configuration>
<configSections>
<!-- define a custom config section to keep appSettings clean -->
<sectionGroup name="myCompany">
<sectionGroup name="myProduct">
<section name="myDll"
type="System.Configuration.SingleTagSectionHandler"
/>
</sectionGroup>
</sectionGroup>
</configSections>
<myCompany>
<myProduct>
<!-- the "type" attribute is "namespace.class, assembly" -->
<myDll setting1="Value1" setting2="value two"
type="MyProject.AspNetStrategy, MyProject" />
</myProduct>
</myCompany>
</configuration>

Imports System.Configuration

Public MustInherit Class StrategyBase

Public MustOverride Sub Something()

Public Shared Function GetStrategy() As StrategyBase
Dim myDllConfig As IDictionary
Dim value1 As String
Dim value2 As String
Dim strategyTypeName As String

myDllConfig =
DirectCast(ConfigurationSettings.GetConfig("myCompany/myProduct/myDll"),
IDictionary)
value1 = DirectCast(myDllConfig("setting1"), String)
value2 = DirectCast(myDllConfig("setting2"), String)
strategyTypeName = DirectCast(myDllConfig("type"), String)

Dim strategyType As Type = Type.GetType(strategyTypeName)
Dim strategyObject As Object =
Activator.CreateInstance(strategyType)
Return DirectCast(strategyObject, StrategyBase)
End Function

End Class

Public Class AspNetStrategy
Inherits StrategyBase

Public Overrides Sub Something()
' Do "Something" for ASP.NET apps
End Sub

End Class

Public Class WinFormsStrategy
Inherits StrategyBase

Public Overrides Sub Something()
' Do "Something" for Win Forms apps
End Sub

End Class

In the above "value1" & "value2" could optionally be used as parameters to
the specific StrategyBase object.



See the following on how to create new sections via the configSections
section.

http://msdn.microsoft.com/library/d...de/html/cpconconfigurationsectionhandlers.asp

and:
http://msdn.microsoft.com/library/d...ref/html/gngrfconfigurationsectionsschema.asp

Also read about the System.Configuration.ConfigurationSettings class and
other classes in the System.Configuration namespace.


Hope this helps
Jay



|I have an class library, which I shared with some of my apps.
| Some of them are Winfroms apps, others are ASP .NET.
| How could I check from a method in my library, is it called form an
| Winforms or ASP .NET?
|
| tnx
|
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top