鉴于以下计划:
using System;
using System.Collections.Generic;
namespace ConsoleApplication49
{
using FooSpace;
class Program
{
static void Main(string[] args)
{
IEnumerable<FooBase> foos = FooFactory.CreateFoos();
foreach (var foo in foos)
{
HandleFoo(foo);
}
}
private static void HandleFoo(FooBase foo)
{
dynamic fooObject = foo;
ApplyFooDefaults(fooObject);
}
private static void ApplyFooDefaults(Foo1 foo1)
{
foo1.Name = "Foo 1";
Console.WriteLine(foo1);
}
private static void ApplyFooDefaults(Foo2 foo2)
{
foo2.Name = "Foo 2";
foo2.Description = "SomeDefaultDescription";
Console.WriteLine(foo2);
}
private static void ApplyFooDefaults(Foo3 foo3)
{
foo3.Name = "Foo 3"; …Run Code Online (Sandbox Code Playgroud) 我最近遇到了这篇SO文章并根据我的场景进行了调整:
using System;
using System.Collections.Generic;
namespace ConsoleApplication18
{
class Program
{
static void Main(string[] args)
{
Manager mgr = new Manager();
var obj = new byte[1024];
var refContainer = new RefContainer();
refContainer.Target = obj;
obj = null;
mgr["abc"] = refContainer.Target;
GC.Collect(GC.MaxGeneration, GCCollectionMode.Forced);
Console.WriteLine(mgr["abc"] != null); // true (still ref'd by "obj")
refContainer = null;
GC.Collect(GC.MaxGeneration, GCCollectionMode.Forced);
Console.WriteLine(mgr["abc"] != null); // false (no remaining refs)
}
}
class RefContainer
{
public object Target { get; set; }
}
class …Run Code Online (Sandbox Code Playgroud) 我看到的是,我的性能计数器实例被添加到超出指定计数器的性能类别中的其他计数器。

鉴于以下代码:
using System;
using System.Diagnostics;
using System.Threading;
using System.Threading.Tasks;
namespace ConsoleApplication26
{
class Program
{
static void Main(string[] args)
{
string category = "Foo";
string categoryHelp = "Test counters";
string fooCounter1Name = "Test Foo counter 1";
string fooCounter1InstanceName = fooCounter1Name + "Instance";
string fooCounter2Name = "Test Foo counter 2";
string fooCounter2InstanceName = fooCounter2Name + "Instance";
if (PerformanceCounterCategory.Exists(category))
PerformanceCounterCategory.Delete(category);
var counterCreationDataCollection = new CounterCreationDataCollection();
counterCreationDataCollection.Add(new CounterCreationData(fooCounter1Name, "", PerformanceCounterType.RateOfCountsPerSecond64));
counterCreationDataCollection.Add(new CounterCreationData(fooCounter2Name, "", PerformanceCounterType.RateOfCountsPerSecond64));
PerformanceCounterCategory.Create(category, categoryHelp, PerformanceCounterCategoryType.MultiInstance, counterCreationDataCollection);
PerformanceCounter fooCounter1Instance = new …Run Code Online (Sandbox Code Playgroud) 我正在使用Visual Studio客户端工具在命令行实用程序中调用VSTS REST API。对于不同的命令(复制,删除,应用策略等),此实用程序可以运行多次。
我正在像这样创建VssConnection
public static VssConnection CreateConnection(Uri url, VssCredentials credentials = null)
{
credentials = credentials ?? new VssClientCredentials();
credentials.Storage = new VssClientCredentialStorage();
var connection = new VssConnection(url, credentials);
connection.ConnectAsync().SyncResult();
return connection;
}
Run Code Online (Sandbox Code Playgroud)
根据文档,这应该是缓存凭据,以便在运行命令行工具时不会再次提示您。但是,每当我运行命令行实用程序并且VssConnection尝试连接时,我都会收到提示。
无论如何,是否有缓存凭据的信息,以便每次运行命令行时都不会提示用户?
应该注意的是,如果我不处理VssConnection,它将在下次运行时不提示。
更新 要明确的是,一旦将对象附加到VssConnection对象后创建连接,问题就不会缓存VssClientCredentials实例。问题是在程序执行之间(即在本地计算机上)缓存用户令牌,以便下次从命令行执行实用程序时,用户不必再次键入其凭据。类似于每次启动时不必始终登录Visual Studio的方式。
我System.DirectoryServices.AccountManagement用来查询用户,然后找到该用户的组.
var _principalContext = new PrincipalContext(ContextType.Domain, domainAddress, adContainer, adQueryAccount, adQueryAccountPassword);
var user = UserPrincipal.FindByIdentity(_principalContext, IdentityType.SamAccountName, account);
var userGroups = user.GetGroups();
foreach (var group in userGroups.Cast<GroupPrincipal>())
{
//////////////////////////////////////////////////////
// getting the underlying DirectoryEntry shown
// to demonstrate that I can retrieve the underlying
// properties without the exception being thrown
DirectoryEntry directoryEntry = group.GetUnderlyingObject() as DirectoryEntry;
var displayName = directoryEntry.Properties["displayName"];
if (displayName != null && displayName.Value != null)
Console.WriteLine(displayName.Value);
//////////////////////////////////////////////////////
Console.WriteLine(group.DisplayName);// exception thrown here...
}
Run Code Online (Sandbox Code Playgroud)
我可以获取底层DirectoryEntry对象并转储其属性和值,但只要GroupPrincipal.DisplayName …
在对RealProxy基础构造函数的调用中,传递要代理的目标对象的Type.我想要做的是动态地将接口添加到代理类型,以便可以将结果代理类型强制转换为其他接口.
例如:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Reflection.Emit;
using System.Runtime.Remoting.Messaging;
using System.Runtime.Remoting.Proxies;
namespace ConsoleApplication17
{
class Program
{
static void Main(string[] args)
{
MyProxy<IFoo> proxy = new MyProxy<IFoo>(new Foo());
IFoo proxiedFoo = (IFoo)proxy.GetTransparentProxy();
// make a proxied call...
proxiedFoo.DoSomething();
// cast proxiedFoo to IDisposable and dispose of it...
IDisposable disposableFoo = proxiedFoo as IDisposable;
// disposableFoo is null at this point.
disposableFoo.Dispose();
}
}
}
public interface IFoo
{
void DoSomething();
}
public class Foo : …Run Code Online (Sandbox Code Playgroud) 我想使用配置文件(App.Config)指定MongoClient连接字符串。是否有内置的方法可以执行此操作,或者我可以只使用ConnectionStringsSection吗?