小编Jim*_*Jim的帖子

使用动态设置不受控制(第三方)密封类型的不同属性

鉴于以下计划:

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)

.net c# oop dynamic c#-4.0

5
推荐指数
1
解决办法
192
查看次数

为什么使用Object Initializer保持对象存活?

我最近遇到了这篇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)

c# garbage-collection weak-references object-initializers

5
推荐指数
1
解决办法
358
查看次数

跨越不同性能计数器的性能计数器实例

我看到的是,我的性能计数器实例被添加到超出指定计数器的性能类别中的其他计数器。

在此处输入图片说明

鉴于以下代码:

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)

c# performancecounter

4
推荐指数
1
解决办法
1028
查看次数

VSTS的VssConnection总是提示输入凭据

我正在使用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的方式。

c# caching visual-studio azure-devops azure-devops-rest-api

4
推荐指数
1
解决办法
2096
查看次数

GroupPrincipal throw"System.Runtime.InteropServices.COMException(0x8007200A):指定的目录服务属性或值不存在."

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 …

active-directory account-management c#-4.0 groupprincipal

3
推荐指数
1
解决办法
4409
查看次数

如何动态组合两个接口传递给RealProxy

在对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)

c# reflection.emit dynamic realproxy

3
推荐指数
1
解决办法
2097
查看次数

如何对MongoDb(MongClient)连接使用配置?

我想使用配置文件(App.Config)指定MongoClient连接字符串。是否有内置的方法可以执行此操作,或者我可以只使用ConnectionStringsSection吗?

c# mongodb

1
推荐指数
1
解决办法
2863
查看次数