Imports System.Data.SqlClient
Imports System.Security.Permissions
Public Class Cache
Private Shared _dict As Dictionary(Of String, String)
'==============================================================================================
Shared Sub New()
Dim p As SqlClientPermission = New SqlClientPermission(PermissionState.Unrestricted)
p.Assert()
_dict = New Dictionary(Of String, String)
End Sub
'==============================================================================================
Public Shared Function [Set](ByVal name As String, ByVal value As String) As Boolean
Dim p As SqlClientPermission = New SqlClientPermission(PermissionState.Unrestricted)
p.Assert()
Dim result As Boolean = False
name = name.ToUpper()
Try
If value IsNot Nothing Then
If _dict.ContainsKey(name) Then
_dict.Item(name) = value
result = True
Else
_dict.Add(name, value)
End If
End If
Catch ex As Exception
'ignore exceptions
End Try
Return result
End Function
'==============================================================================================
Public Shared Function [Get](ByVal name As String) As String
Dim p As SqlClientPermission = New SqlClientPermission(PermissionState.Unrestricted)
p.Assert()
Dim result As String = ""
name = name.ToUpper()
Try
If _dict.ContainsKey(name) Then
result = _dict.Item(name)
End If
Catch ex As Exception
'ignore exceptions
End Try
Return result
End Function
End Class
using System;
using System.Collections.Generic;
using System.Text;
using System.Security.Permissions;
using System.Data.SqlClient;
namespace MyNamespace
{
public static class Cache
{
private static Dictionary<string, string> _dict;
//-----------------------------------------------------------------------------------------
static Cache()
{
new SqlClientPermission(PermissionState.Unrestricted).Assert();
_dict = new Dictionary<string, string>();
}
//-----------------------------------------------------------------------------------------
public static bool Set(string name, string value)
{
new SqlClientPermission(PermissionState.Unrestricted).Assert();
bool result = false;
name = name.ToUpper();
try
{
if (value != null)
{
if (_dict.ContainsKey(name))
{
_dict[name] = value;
result = true;
}
else
{
_dict.Add(name, value);
result = true;
}
}
}
catch {/* ignore exceptions */}
return result;
}
//-----------------------------------------------------------------------------------------
public static string Get(string name)
{
new SqlClientPermission(PermissionState.Unrestricted).Assert();
name = name.ToUpper();
string result = "";
try
{
if (_dict.ContainsKey(name))
result = _dict[name];
}
catch {/* ignore exceptions */}
return result;
}
}
}